ola pessoal gostaria de sabe se alguem pode me disse como eu posso fazer um metodo para meu projeto que tenha com objetivo BUSCA, tipo preciso que ao digita o nome da pessoal cadastrada esse metodo me mostre todos os dados dessa pessoal. para tenta facilita vou coloca meu projeto dai se alguem tiver alguma ideia eu agradeço. :roll:
import java.io.*;
import java.util.StringTokenizer;
public class Biblioteca{
public void Cadastrar()throws IOException{
Biblioteca x = new Biblioteca();
String dados;
dados = x.Ler();
String vetor[] = new String[5];
int c = 0;
BufferedReader ler = new BufferedReader( new
InputStreamReader(System.in));
System.out.println("Digite o Nome: ");
vetor[0] = ler.readLine();
System.out.println("Digite o CPF: ");
vetor[1] = ler.readLine();
System.out.println("Digite o Endereço: ");
vetor[2] = ler.readLine();
System.out.println("Digite o Cor: ");
vetor[3] = ler.readLine();
System.out.println("Digite o Nascimento: ");
vetor[4] = ler.readLine();
dados += vetor[0] + ";" + vetor[1] + ";" + vetor[2] + ";" + vetor[3] + ";" + vetor[4];
this.Salvar(dados);
}
public void Salvar(String a)throws IOException{
try{
RandomAccessFile file = new RandomAccessFile( "Clientes.doc", "rw" ); // rw serve para ler e salvar arquivo
file.writeUTF(a.trim());
file.close();
}
catch(Exception e){
}
}
public String[] AbrirArquivo(){ // Método que recebe um parâmetro, cuja função é abrir um arquivo
String[] V;
int cont = 0;
String line_str;
try{
BufferedReader data = new BufferedReader(new FileReader("Clientes.doc"));
while((line_str = data.readLine()) != null)
cont++;
data.close( );
}catch (IOException e) {}
V = new String[cont];
cont = 0;
try{
BufferedReader data = new BufferedReader(new FileReader("Clientes.doc"));
while((line_str = data.readLine()) != null) {
V[cont] = line_str;
cont++;
}
data.close();
} catch (IOException e) {}
String temp;
int max = V.length;
for(int i=0; i<max-1; i++)
for (int j=i+1; j<max; j++) {
if (V[i].compareTo(V[j])>0) {
temp = V[i];
V[i] = V[j];
V[j] = temp;
}
}
return V;
}
public String Ler(){
String line_str;
String Arquivo = "";
try{
BufferedReader data = new BufferedReader(new FileReader("Clientes.doc"));
while((line_str = data.readLine()) != null)
Arquivo += line_str + "\n";
data.close( );
}catch (IOException e) {}
return Arquivo;
}
public static void main (String args[]) throws IOException{
Biblioteca x = new Biblioteca();
BufferedReader ler = new BufferedReader( new InputStreamReader(System.in));
int c;
do{
c = x.menu_select();
switch(c){
case 1: x.Cadastrar();
break;
case 2: x.Listar();
break;
case 3: System.exit(0);
}
}while (c != 3);
}
public int menu_select() throws IOException{
BufferedReader ler = new BufferedReader( new InputStreamReader(System.in));
String c;
int a;
System.out.println("\n 1. Cadastrar");
System.out.println(" 2. Listar");
System.out.println(" 3. Sair\n");
do{
System.out.print("Informe sua escolha: ");
c = ler.readLine();
a = Integer.parseInt(c);
} while (a < 0 || a > 3);
return a;
}
public void Listar(){
Biblioteca x = new Biblioteca();
String V[] = x.AbrirArquivo();
int c = 0, contador = 0;
System.out.println("Nome\tCPF\tEndereço\tCor\tNascimento");
while (c < V.length){
StringTokenizer capturador = new StringTokenizer(V[c],";");
String leitorDados[] = {"","","","",""};
while (capturador.hasMoreTokens()) {
leitorDados[contador]= capturador.nextToken();
contador++;
}
System.out.println(leitorDados[0] + "\t" + leitorDados[1] + "\t" + leitorDados[2] + "\t\t" + leitorDados[3] +"\t" + leitorDados[4]);
c++;
contador = 0;
}
}
}
falow
[size=“11”][color=“red”]* Editado: Lembre-se de utilizar BBCode em seus códigos - Matheus [/color][/size] :joia: