• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMap for username and password in text file

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone ! I have a trouble and can't fix it. I have one file.txt where I store usernames and passwords. I used split to get username and password into HashMap.
I need to keyboard input username and password, if both match with username and password in my text file, I get message "You got success", else if I put wrong password I get anoter 3 chances to put password, or my account will be blocked.

In the end, I want to enter both username and password wrong so I enter in a loop to be able to write them again for 5 times. My trouble starts here, because I enter wrong username and password, then I get message to put them again. If I put them again, even if I put right username and password, it won't recognize them . Here is my code:

 
Ranch Hand
Posts: 954
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes because at first time when you enter wrong username, then it sets password null as per line 24. So your variable passwordInFile is null.
As all the condition is false so only else loop is called and ask you to enter username and password again. But as you comparing it again with
passwordInFile which already null it goes to else if loop.

Once loop continues and index reaches at 1 it enters in if loop, print "Please register" and come out to the loop due to break statement.

So, program is working as you written not what you want. So, i am suggesting you first think and write down on paper how you want to do it and
then start coding..
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Mann Claudiu!

The problem lies in that your main method does far too much. Break up your program in steps and make separate methods for those steps.
  1. Prompt a user for username.
  2. Retrieve stored credentials for the given username.
  3. Prompt a user for password.
  4. Compare the entered password with the stored credentials.
  5. Authenticate a user by trying steps 3-4 three times.
These can all easily be separate methods. Your authenticate method could look something like this:

Do you see how modular code like this is much easier to read and maintain?
 
Mann Claudiu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:Yes because at first time when you enter wrong username, then it sets password null as per line 24. So your variable passwordInFile is null.
As all the condition is false so only else loop is called and ask you to enter username and password again. But as you comparing it again with
passwordInFile which already null it goes to else if loop.

Once loop continues and index reaches at 1 it enters in if loop, print "Please register" and come out to the loop due to break statement.

So, program is working as you written not what you want. So, i am suggesting you first think and write down on paper how you want to do it and
then start coding..



Thanks for your reply, I know how is working, I want to know what I have to do, because I lost 15 hours thinking about what to do.
 
Mann Claudiu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Welcome to CodeRanch, Mann Claudiu!

The problem lies in that your main method does far too much. Break up your program in steps and make separate methods for those steps.

  1. Prompt a user for username.
  2. Retrieve stored credentials for the given username.
  3. Prompt a user for password.
  4. Compare the entered password with the stored credentials.
  5. Authenticate a user by trying steps 3-4 three times.
These can all easily be separate methods. Your authenticate method could look something like this:

Do you see how modular code like this is much easier to read and maintain?



Thank you ! Yes I see that your code is easier to read and maintain. I will try to make my code like this
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic