DevMedia - asp.net, Java, Delphi, SQL e web Design, tudo em um só lugar!
Bem vindo a DevMedia!
LOGIN:     SENHA:
 
 

Fórum DevMedia


Autor
Mensagem
FREDERICO BRIGATTE***
 


País: Brasil
Estado: SP
Cidade: Saõ José dos Campos
Mensagens: 1025
 Postado em: 15/3/2012 03:39:21 PM
 
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:

#Código

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***
 


País: Brasil
Estado: SP
Cidade: Saõ José dos Campos
Mensagens: 1025
 Postado em: 15/3/2012 03:39:56 PM
 
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***
 


País: Brasil
Estado: SP
Cidade: Saõ José dos Campos
Mensagens: 1025
 Postado em: 15/3/2012 03:40:38 PM
 

Citação:
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:

#Código

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.
Davi Gomes da Costa
 

 


País: Brasil
Estado: CE
Cidade: Fortaleza
Mensagens: 1770
 Postado em: 16/3/2012 08:26:43 AM
 
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
DYEGO SOUZA DO CARMO
 

 


País: Brasil
Estado: PR
Cidade: xxxxx
Mensagens: 1836
 Postado em: 14/4/2012 11:53:54 AM
 
Deu certo ?

Valeu !
web-03
DevMedia  |  Anuncie  |  Fale conosco
Hospedagem web por Porta 80 Web Hosting
2013 - Todos os Direitos Reservados a web-03