Pesquisar em txt
15/03/2012
0
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***
Posts
15/03/2012
Frederico Brigatte***
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);
}
}
15/03/2012
Frederico Brigatte***
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.
16/03/2012
Davi Costa
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
Clique aqui para fazer login e interagir na Comunidade :)