Galera tenho que fazer uma pesquisa de opinião em Java, onde todos os possíveis dados, perguntas e respostas devem ser lidos do teclado; estes dois últimos, com limitações feitas pelo usuário...
No entanto não estou conseguido faze-lo, até tenho classes prontas como pessoa, usuário, administrador, pesquisa, pergunta, resposta, gerenciapesquisa e a principal. Mas estou com um grande problema na hora de gravar uma pergunta e, que foi fornecida pelo administrador, suas possíveis respostas, que também devem ser fornecidas pelo administrador.
meu codigo GerenciaPesquisaimport java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
public class GerenciaPesquisa {
Vector respostas;
Perguntas pergunta1, pergunta2;
public GerenciaPesquisa(Perguntas pergunta1, Perguntas pergunta2) {
respostas = new Vector();
this.pergunta1 = pergunta1;
this.pergunta2 = pergunta2;
}
public boolean registraResposta(Respostas nRespostas)
{
respostas.add(nRespostas);
return true;
}
public boolean registrarArquivo(){
FileOutputStream arq;
BufferedOutputStream buffer;
DataOutputStream dados = null;
Respostas aux;
try {
arq = new FileOutputStream("c:\perguntas.txt");
buffer = new BufferedOutputStream (arq);
dados = new DataOutputStream(buffer);
for(int cont = 0; cont < respostas.size(); cont++){
aux = (Respostas)respostas.get(cont);
dados.writeUTF(aux.getPergunta().getNome());
dados.writeInt(cont + 1);
dados.writeUTF(aux.getPergunta().getNome());
dados.writeUTF(aux.getPergunta().getResposta());
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally{
if(dados != null)
try {
dados.close();
return true;
} catch (IOException e)
{
e.printStackTrace();
}
}
return false;
}
public boolean lerPerguntas() {
FileInputStream arq;
BufferedInputStream buffer;
DataInputStream dados = null;
try {
arq = new FileInputStream("c:\perguntas.txt");
buffer = new BufferedInputStream(arq);
dados = new DataInputStream(buffer);
while(dados != null){
System.out.print(dados.readUTF());
System.out.print(" " + dados.readInt());
System.out.print(" = " + dados.readUTF());
System.out.println(" " + dados.readInt());
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
//catch (EOFException e)
// permite a exceção para sair do laço }
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if(dados != null)
try {
dados.close();
return true;
} catch (IOException e) { e.printStackTrace(); }
}
return false;
}
}
Se alguém puder me dar uma ajuda ai agradeço.......
o objetivo é gravar a pergunta e suas n respostas.....