• Post Reply Bookmark Topic Watch Topic
  • New Topic

user input of a key and value into a hashmap  RSS feed

 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Citizens,

I have a hashmap and I want to allow a user to enter an additional key and value.  So I added a if else statement to ask if the user wants to add another key, in this case, name.  So here is where I'm getting stuck.  I know how to do that with an integer, string, double, etc., but can't find a way to do that with a hashmap key.  I've done some research, including the API and came up empty.  Line 6 below is where I'm smacking into the wall.  Thanks.


 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you use in.nextLine().

IF "yes"
    Prompt for name
    nextLine()
    Prompt for grade
    nextLine()
    map.put(name,grade)
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need nextLine() twice because it is preceded by next().
 
Alex Houser
Ranch Hand
Posts: 41
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, I started to look at this but I'm tired so I'll pick it up again tomorrow.  I appreciate the help.
 
Alex Houser
Ranch Hand
Posts: 41
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So this is my updated code and it works.  Then it dawned on me that I need to allow the user to select YES again until they are done adding users.  Can I do that without totally reconstructing this?  A reminder to plan before starting to code...

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use next() in line 8, there is no need for the nextLine() call in line 6.
Under what circumstances will the else if be executed? Is there any point in having it at all?
How are you going to repeat the code? I can't see anything which would repeat.
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to give the user the option of adding another name, right now they enter one and it moves on to the next task, which is removing a name.

I want to ask them after they enter a name if they want to add another name.
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



If you use next() in line 8, there is no need for the nextLine() call in line 6.
Under what circumstances will the else if be executed? Is there any point in having it at all?
How are you going to repeat the code? I can't see anything which would repeat.


Here is my whole program, if I delete line 6 then my modify grade statement doesn't work properly.  Any critiques or suggestions for improvement are welcome.

Thanks






 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* You can simplify loops over Maps like this:

* You're using raw types.  Be sure to use the generic form of the class.  Notice in the above code, it's Map.Entry<String, String> me.

* Don't use variable names like "list", especially if they're Maps.  "studentGrades" would be better.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* You have some code in the class twice that is exactly the same.  Put it into a method.

* Variable names like "a" and "b" are not recommended.  Use names that say what the variables are.

* (minor) You can use System.out.print("Prompt: ") to keep the cursor at the end of the prompt.  It's a matter of style, not a problem.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed that you add Cara twice to the map.  That's not going to work if you use a map.  You might want a List instead, but then changing and removing grades gets trickier.

I also noticed that you're using String for the grade.  Are you sure that's the best type for it?  What if you wanted to write if (grade > 90) ?
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!