Compare two split strings of a FILE file

1

I am doing an exercise in which your goal is to enter a file with File, read it and check with a split if the two words that are between the symbol " | " are equal (letter by letter). I tried to do it with a "charAt" but it does not work out at all.

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

            File ips = new File("C:\Users\Victor\Desktop\e.txt");

            Scanner teclado=null;
            String texto="";

            try {
                teclado=new Scanner(new FileReader(ips));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            while (teclado.hasNextLine()){
                texto+=teclado.nextLine() + "\n";
            }

            String[] separacion = texto.split("\n");
            for (int i = 0; i < separacion.length; i++) {
                String[] separacion2 = separacion[i].split("\|");
                for (int j = 0; j < separacion2.length; j++) {

                }
            }
            teclado.close();
        }
}

In the text file I have this:

Hello | hi

Hello | jewel

Hello | Hello

Hello | Hole

    
asked by Victor Matilla Sanchez 10.06.2018 в 14:25
source

1 answer

1

You do not need to add a line break "\ n" in fact when reading the value of each line using .nextLine() , you can make the comparison by means of the method .equals() and print the result.

   while (teclado.hasNextLine()){
                //Obtiene la linea de texto
                texto = teclado.nextLine();
                //Crea un array donde se almacenan ambas palabras.
                String[] separacion = texto.split("\|");

                //Realiza la comparacion de ambas palabras
                if(separacion[0].equals(separacion[1])) {//iguales
                  System.out.println(separacion[0] + " es igual a " + separacion[1]);
                }else{//diferentes
                    System.out.println(separacion[0] + " es diferente a " + separacion[1]);
                }

            }

This would be the complete code

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

            File ips = new File("C:\Users\Victor\Desktop\e.txt");

            Scanner teclado=null;
            String texto="";

            try {
                teclado=new Scanner(new FileReader(ips));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            while (teclado.hasNextLine()){
                //Obtiene la linea de texto
                texto = teclado.nextLine();
                //Crea un array donde se almacenan ambas palabras.
                String[] separacion = texto.split("\|");

                //Realiza la comparacion de ambas palabras
                if(separacion[0].equals(separacion[1])) {//iguales
                  System.out.println(separacion[0] + " es igual a " + separacion[1]);
                }else{//diferentes
                    System.out.println(separacion[0] + " es diferente a " + separacion[1]);
                }

            }
            teclado.close();

        }
}

will have as output

Hola es diferente a hola
Hola es diferente a joya
Hola es igual a Hola
Hola es diferente a Hole
    
answered by 10.06.2018 / 17:14
source