Pesquisar em txt

Java

15/03/2012

Como posso fazer uma pesquisa num arquivo txt, entro com o nome ou parte e retorna o nome e telefone na tela? O arquivo tem somente nome e telefone.

Achei esse código na net, só que mostra todos:

    import javax.swing.*;  
    import java.io.*;  
      
      
    public class PesquisaNome{  
      
    public static void main(String [] args){  
      
    File bd = new File (cadastro.txt);  
    int flag=0;  
    if (bd.exists()){  
    try{  
    FileReader fr = new FileReader(bd);  
    BufferedReader ler = new BufferedReader (fr);  
    String temp=, a=, nome=;  
    nome = JOptionPane.showInputDialog(Digite o Nome:);  
      
    while ((temp=ler.readLine())!=null){  
    a += temp+\n;  
    if (temp.equals(Nome: +nome))  
    flag = 1;  
      
    }  
      
    if (flag == 1)  
    JOptionPane.showMessageDialog(null,a);  
    else  
    JOptionPane.showMessageDialog(null,Nome não encontrado);  
      
    }  
    catch (IOException ex){  
    JOptionPane.showMessageDialog(null, Erro na leitura);  
    }  
      
    }  
    System.exit(1);  
      
    }  
    }   

Frederico Brigatte***

Frederico Brigatte***

Curtidas 0

Respostas

Frederico Brigatte***

Frederico Brigatte***

15/03/2012

import javax.swing.*;
import java.io.*;


public class PesquisaNome{

public static void main(String [] args){

File bd = new File (cadastro.txt);
int flag=0;
if (bd.exists()){
try{
FileReader fr = new FileReader(bd);
BufferedReader ler = new BufferedReader (fr);
String temp=, a=, nome=;
nome = JOptionPane.showInputDialog(Digite o Nome:);

while ((temp=ler.readLine())!=null){
a += temp+\n;
if (temp.equals(Nome: +nome))
flag = 1;

}

if (flag == 1)
JOptionPane.showMessageDialog(null,a);
else
JOptionPane.showMessageDialog(null,Nome não encontrado);

}
catch (IOException ex){
JOptionPane.showMessageDialog(null, Erro na leitura);
}

}
System.exit(1);

}
}
GOSTEI 0
Frederico Brigatte***

Frederico Brigatte***

15/03/2012

Como posso fazer uma pesquisa num arquivo txt, entro com o nome ou parte e retorna o nome e telefone na tela? O arquivo tem somente nome e telefone.

Achei esse código na net, só que mostra todos:

    import javax.swing.*;  
    import java.io.*;  
      
      
    public class PesquisaNome{  
      
    public static void main(String [] args){  
      
    File bd = new File (cadastro.txt);  
    int flag=0;  
    if (bd.exists()){  
    try{  
    FileReader fr = new FileReader(bd);  
    BufferedReader ler = new BufferedReader (fr);  
    String temp=, a=, nome=;  
    nome = JOptionPane.showInputDialog(Digite o Nome:);  
      
    while ((temp=ler.readLine())!=null){  
    a += temp+\n;  
    if (temp.equals(Nome: +nome))  
    flag = 1;  
      
    }  
      
    if (flag == 1)  
    JOptionPane.showMessageDialog(null,a);  
    else  
    JOptionPane.showMessageDialog(null,Nome não encontrado);  
      
    }  
    catch (IOException ex){  
    JOptionPane.showMessageDialog(null, Erro na leitura);  
    }  
      
    }  
    System.exit(1);  
      
    }  
    }   


postei novamente o código, saiu errado. Está abaixo.
GOSTEI 0
Davi Costa

Davi Costa

15/03/2012

import javax.swing.*;
import java.io.*;

public class PesquisaNome {

public static void main(String[] args) {

File bd = new File(cadastro.txt);
int flag = 0;
if (bd.exists()) {
try {
FileReader fr = new FileReader(bd);
BufferedReader ler = new BufferedReader(fr);
String temp = , a = , nome = ;
nome = JOptionPane.showInputDialog(Digite o Nome:);

while ((temp = ler.readLine()) != null) {
a += temp + \n;
//ou se preferir mostarr só a linha em caso do arquivo ser muito grande
//a = temp;
if (temp.contains(nome))
flag = 1;
}

if (flag == 1)
JOptionPane.showMessageDialog(null, a);
else
JOptionPane.showMessageDialog(null, Nome não encontrado);

} catch (IOException ex) {
JOptionPane.showMessageDialog(null, Erro na leitura);
}

}
System.exit(1);

}
}


att Davi
GOSTEI 0
Dyego Carmo

Dyego Carmo

15/03/2012

Deu certo ?

Valeu !
GOSTEI 0
POSTAR