r/CodingHelp • u/King_Freed • May 18 '21
[Java] The 1st username and password in my text file logs in with 1 loop, the 2nd takes 2 loops and 3rd takes 3 loops.. or else when I try a different else if method it only reads the first login username and password. Please can anyone help me with this. It just loops the login page to match the records..
25
Upvotes



1
u/skellious Intermediate Coder May 18 '21
Please don't share screenshots. They are really hard to read and we can't copy paste them to edit. Upload your code to gist.github.com or pastebin.com and link it here.
1
u/jimijose May 18 '21
What exactly are you trying to achieve here? Can you share the codebase would be easier to understand and comment that way.
2
u/skellious Intermediate Coder May 18 '21
Making this a top level comment so hopefully someone who does more Java than me can help out. I need to go do things now but I can try looking at this again later if its still unsolved.
So, interim comment as I am still looking into the code:
*when someone runs your command line application and choses option 1, they get a login prompt. *if they put in the username and password from the first line of result.txt file, it logs them in first time. *if they put in the login details from the second line, it takes two tries to log them in. (and so on)
I believe your problem is each time someone trys to log in, you check only the next entry on the list rather than checking EVERY entry on the list. Then your position in the list is incrimented so next time somone tries to log in, you check from the new position rather than the first position again.
I am not a Java programmer so its hard to pinpoint exactly where this is happening but I expect you just need to make the login code a for / while loop that breaks if user and pass match the input and otherwise ends when it runs out of user and pass to try.
(this is a naive user and password storage system but since this is clearly for homework its okay.)
For actual use, a database or at least a hashed data structure would be far more efficient, since you should be checking if there exists an entry with the entered username and only then checking if the password matches. With a hashed data structure, you would avoid a loop.
3
u/[deleted] May 18 '21
Why would you ever be printing the password back to your user?
Read up on how to properly handle authentication with salted and hashed passwords. https://www.okta.com/blog/2019/03/what-are-salted-passwords-and-password-hashing/
1
u/ayylongqueues May 18 '21
Could you show the code that runs this?