search for a word in a txt file

2

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.

    
asked by Hello There 21.09.2018 в 13:02
source

1 answer

0

The problem is in the else statement that you make a break to the process and stop reading the file, giving the option to only leave the data of the first person.

Remove break; or even else .

try {
    do {
        BufferedReader br=new BufferedReader(new FileReader(fichero));
        System.out.println("Introduco un nombre que quieras buscar: ");
        respuesta="Persona: "+sc.nextLine();

        String linea="";
        boolean encontrado = false;
        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());
                }   
                encontrado = true;
                break;

            }

        }

        if(!encontrado) System.out.println("El usuario no existe");

            System.out.println("¿Quieres introducir otro nombre?");
            respuesta=sc.nextLine();



    }while(respuesta.equalsIgnoreCase("si"));
} catch (IOException e) {

    System.out.println("Error");
}

Also the br takes the empty lines between data and does not show the values well. Change it to something like this:

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
    
answered by 21.09.2018 / 15:14
source