0

I'm trying to do a simple login from a textfile. I've used different ways of reading the text from the file to a String line(BufferedReader and Scanner). I am able to get the line into a string, but it doesn't want to compare the 2 strings and match when I use an if statement(.equals()) or even if I use .equalsIgnoreCase(). When I print the 2 strings to be compared they are the same. but my if statement doesn't seem to return true? This was the last coding i tried (I thought maybe if I put it into an array it would compare true, but still nothing). Iv'e looked and saw similar questions to comparing strings from textfile, but never saw a problem with the if statement to return true

import java.io.*;
import java.text.*;
import java.lang.*;

public class tes
{

    public static void main(String[] args)throws Exception
    {
        String logline = "JMX^1234";

        ArrayList<String> lines = new ArrayList<String>();
        FileReader fr = new FileReader("/home/jmx/Desktop/javap/Bank/jm.txt");
        BufferedReader br = new BufferedReader(fr);

        String rline = br.readLine();

        while(rline != null)
        {
            lines.add(rline);
            rline = br.readLine();
        }

        String[] users = new String[lines.size()];
        lines.toArray(users);

        for(int i = 0; i < users.length; i++)
        {
            if(logline.equals(users[i]))
            {
                System.out.println("Matched");
            }
        }

        System.out.println("Login line: " + logline);
        System.out.println("Text Line: " + users[0]);

        br.close();
        fr.close();
    }

}

1 Answers1

0

I've tried to execute your code and everything worked as expected. I received "matched". Maybe it's some kind of encoding issue. Try to compare length and if it is ok, try to leave only one line in the file and try this code:

String logline = "JMX^1234";

        ArrayList<String> lines = new ArrayList<String>();
        FileReader fr = new FileReader("/home/jmx/Desktop/javap/Bank/jm.txt");
        BufferedReader br = new BufferedReader(fr);

        String rline = br.readLine();

        while(rline != null)
        {
            lines.add(rline);
            rline = br.readLine();
        }

        String[] users = new String[lines.size()];
        lines.toArray(users);

        for (char ch : users[0].toCharArray()) {
            System.out.print((int)ch);
        }
        System.out.println();

        for (char ch : logline.toCharArray()) {
            System.out.print((int)ch);
        }
        System.out.println();

        for(int i = 0; i < users.length; i++)
        {
            if(logline.equals(users[i]))
            {
                System.out.println("Matched");
            }
        }

        System.out.println("Login line: " + logline);
        System.out.println("Text Line: " + users[0]);

        br.close();
        fr.close();

It should return equal lines of numbers like this:

7477889449505152
7477889449505152
Matched
Login line: JMX^1234
Text Line: JMX^1234

Also try to check out this answer: https://stackoverflow.com/a/4210732/6226118

I.R.
  • 442
  • 1
  • 7
  • 16
  • I've tried running your code. I also just have one line in my textfile - "JMX^1234". Just for testing purposes.But I still can't get matched. Currently I'm running on Linux terminal instead of windows. Could this be causing some encoding erros? – JM Du Plessis Apr 23 '20 at 07:44
  • `652797477889449505152 7477889449505152 Login line: JMX^1234 Text Line: JMX^1234` – JM Du Plessis Apr 23 '20 at 07:52
  • @ApprenticeJM, look at the numbers! They are different. It means that these two strings are different. We've just proved that this is because of encoding. Try to make a file in another editor (e.g. nano). Also, make sure you use the same language while typing (eng). Some symbols can have the same appearance but different ASCII code. – I.R. Apr 23 '20 at 07:57
  • @ApprenticeJM, I've found what you got! The first piece of numbers starts with 65279. It's the Unicode character named 'ZERO WIDTH NO-BREAK SPACE': check out this question: https://stackoverflow.com/questions/9691771/why-is-65279-appearing-in-my-html – I.R. Apr 23 '20 at 08:20
  • Thank you so much. This helped a lot! – JM Du Plessis Apr 23 '20 at 13:11