I have a program that asks the user for a name to look up their data in a file.
The file contains the following:
Persona: Adrian
Apellido: Rodriguez
Edad: 19
Persona: Alejandro
Apellido: Grande
Edad: 28
Persona: Mikel
Apellido: Aparicio
Edad: 21
Persona: Rodrigo
Apellido: Novoa
Edad: 21
Persona: Asier
Apellido: Urangan
Edad: 22
And the code is as follows:
import java.io.*;
import java.util.Scanner;
public class Ficheero4 {
public static void main(String[] args) throws FileNotFoundException {
File fichero = new File ("C:\Users\adminportatil\eclipse-workspace\ADInicio\Ficheros\datos.txt");
Scanner sc=new Scanner(System.in);
String respuesta;
try {
do {
BufferedReader br=new BufferedReader(new FileReader(fichero));
System.out.println("Introduco un nombre que quieras buscar: ");
respuesta="Persona: "+sc.nextLine();
String linea="";
while ((linea= br.readLine())!=null) {
if(linea.equalsIgnoreCase(respuesta)) {
System.out.println(linea);
for(int i=0;i<2;i++) {
System.out.println(br.readLine());
}
break;
}else {
System.out.println("El nombre introducido no existe.");
break;
}
}
do {
System.out.println("¿Quieres introducir otro nombre?");
respuesta=sc.nextLine();
}while(respuesta.equalsIgnoreCase("si")==false&&respuesta.equalsIgnoreCase("no")==false);
}while(respuesta.equalsIgnoreCase("si"));
} catch (IOException e) {
System.out.println("Error");
}
}
}
The fact is that the first round line = br.readline () collects the information well but the second time not and I would like to know why and how to solve it. Thanks for your time.