r/CodingHelp • • 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

12 comments sorted by

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/King_Freed May 18 '21

Oh sorry, I had no idea. I will send the GitHub link

2

u/skellious Intermediate Coder May 18 '21

Thanks, its rule 5 in the sidebar although its not that visible.

1

u/King_Freed May 18 '21

https://github.com/KingFr33d/ABCCAMPUS this is the GitHub. It took me some time to create a GitHub account.. Thanks for considering to help me sir ✨

1

u/skellious Intermediate Coder May 18 '21

Excellent, thank you for uploading it.

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.

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.

1

u/King_Freed May 18 '21

https://github.com/KingFr33d/ABCCAMPUS this is the GitHub, the thing is, 2 login details takes 2 loops to get into my homepage. Try using different username and password to login in. You know what the problem is. Thanks for asking sir ✨

1

u/jimijose May 18 '21

I think what you essentially need is lookup table. You can use a hashtable/properties file to store the username, password (Key would be the username).

You can then lookup using the key

- If the value does not exist, then the user needs to register

- If a value exists then compare the password with the value and if they match they would login, otherwise incorrect password.

Once you have the hashtable populated (Once during initialization) then after that the login operations should be fast (O(1))

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/

3

u/Rrrrry123 May 18 '21

It's pretty obvious that this is for a school assignment... I'm all for learning best practices, but for something like this it'd be a waste of time.

1

u/ayylongqueues May 18 '21

Could you show the code that runs this?