This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Hashmap - creating username and password.

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create a program that allows users to:

1. Create new "users" with a username and password combination
2. "Log in" with any username and password combination.
You must store these username-password combinations in your program, and the program must notify the user if the login was successful or not

For the 1st condition I tried:

But I get InputMismatch the second time and I dont know why?
As for the 2nd condition:

Here I get a hashmap.containsKey is always false.
I dont know why these errors appeared so I want to know what I did wrong exactly.
Do I have the right ideas?  How can I correct this?

 
Jaredson Fetiza
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Woah this my first time using tags, its a mess.
 
Marshal
Posts: 8496
610
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaredson Fetiza wrote:Woah this my first time using tags, its a mess.


Agreed. Well, code formatting and indentation is as is.
 
Saloon Keeper
Posts: 9717
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the "password" I would not have expected you to use hashmap.containsValue(...) as the hashmap may contain duplicate values or the person my have inadvertently entered someone else's password. I expected to see hashmap.get( userename ) somewhere.
 
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also don't like while (true) ... I would suggest a do loop, reading while the input doesn't equal “Quit” or something like that.
I suspect you are falling into a little‑known pitfall with nextLine(). You have doubtless been told it reads the next line, and that is exactly what it doesn't do. Look in this old post of mine. When the loop runs the second time, you are using nextLine() after nextInt(), and the old post explains what can go wrong. Documentation link.
 
Jaredson Fetiza
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kinda late but I was able to do what I wanted with this:

Thanks for the help      
Feel free to tell me if there was a better way I could have gone doing it.
 
Rancher
Posts: 4893
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The { on line 16 (paired with } on line 39) is confusing.  I don't think it does anything.
The message to user:  Enter the number 7 to stop
does not agree with the logic in the while on line 16 which will continue with a 7

Then the next loop uses different logic, it asks user to enter a 7 to continue.
The confusion is the first loop says enter 7 to stop, the second loop says enter 7 to continue.
 
Jaredson Fetiza
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah sorry about that, the curly brackets on line 16 and 39 is something I forgot to delete when I finished the code.
And I understand that the enter 7 can be confusing so I'll try to make it clear next time.
 
Sheriff
Posts: 17293
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That first loop - do { ... } while (input == 7) will continue to execute until you enter something other than 7. However, your instruction is "Enter the number 7 to stop account creation otherwise, enter any number to continue."

So unless you somehow missed this in your testing, somebody else using your program will be led astray by the contradiction between the instructions and what the code is actually doing.
 
Junilu Lacar
Sheriff
Posts: 17293
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Formatted properly, your code would look like this:

The block from 19-42 is probably unintentional but it saved you from having to deal with a compile time error where you try to re-declare the input variable. If you try deleting lines 19 and 42 to remove the block, which is what you'd normally have, you'll see the error when you try to compile.

EDIT: I just noticed that you used a different name, input1 instead of input so no compiler error would have occurred if you deleted lines 19 and 42.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic