简体   繁体   中英

Compare user string input to a string from a textfile

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();
    }

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM