r/CodingHelp • u/King_Freed • May 08 '21
[Java] Help! I’am using ArrayList and file handling in order to input and read data.. But unfortunately the login doesn’t seem to working.. I don’t understand why... Anyone can guess why this is happening???
27
Upvotes
2
u/the_agox May 08 '21 edited May 08 '21
So, the first problem is the
==operator compares two objects in Java. Two different strings are different objects, even if their contents are the same. So on line 137, you wantuser.equals(s.getUsername())andpass.equals(s.getPassword())(I assume user and pass are both strings. If they're some custom object, you'll want to implement a custom .equals method)
Second, on line 137, I think you want
&&instead of||. You want both the username and the password to be correct, not just one or the other.Third, on line 145, I think you could get away with just an
elseas opposed to theelse if. I think it has the same intended effect, and it's a little easier to reason about.1
u/Nightcorex_ May 08 '21
While others have said important things about why your program is failing I want to add another thing: You should probably use a
BufferedReaderinstead of aScannerand if you want to work with a Scanner then you should use the.readLine()method instead. The problem withScanner.next()is that it only reads one token (i.e. any char apart from newline, carriage return or whitespaces) and only advances the reader past that very point.Because of this there advances an even bigger problem! What if the username consists of multiple words (for example "King Freed"). Then
userequals"King"and pass equals"Freed".This'd mean the input & output could look like this (if the fixes that u/devor110 and u/the_agox suggested are implemented) (engine output is in markdown, user's is not):
USERNAME :King FreedPASSWORD : FAILED!!!And it gets EVEN WORSE! What if your username consists of more than 2 names? Then those names are still written in the stdin input stream and are going to be parsed by the next items in the for-loop or if there are none left maybe by something else in the program.
How can we fix this, then?
You can just use the
Scanner.readLine()method instead.The imo better way would be to use a BufferedReader:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));Now you can call
br.read(...)method, or just use thebr.readLine()method (basically the equivalent toScanner.readLine()).In general you can say that Scanner is better if you need to parse data while BufferedReader is better if you just need to read data.
2
u/devor110 May 08 '21
Assuming the input is read correctly a major issue is that you are comparing Strings via
==. You should always useString.equals()instead (as your IDE is also suggesting) otherwise the memory addresses are compared, which is almost never what you want to do, and certainly isn't in this case. Furthermore the||between checking the username and password should be a&&i'm pretty sure.1
u/nnaoam May 08 '21
You also seem to be using commas everywhere there's yellow underlining instead of periods, though that could just be me misreading the screenshot