• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMaps  RSS feed

 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I need help regarding my assignment...i am beginner in Java....my query is like: "i want to create hashmaps where the inner hashmap contains the key as firstname and value as a Class Student which contains parameters firstname, lastname,etc. I need this hashmap to be accessed by other objects of different classes". Please, help me out?



Here I have a main class


As we give the values by Scanner, it should be Stored in Hashmap and how those values can be accessed by other object classes?
How does the Logic works? What my next step?
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to take those values and create a Student instance. There's a suitable constructor on Student that allows you to do this.
Then you need to put the Student (and Key) into the Map, using a suitable method on the java.util.Map API (there's a hint in this sentence in fact).

One thing, the standard Java code style is that classes start with a Capital (which you do, which is great), but variables start with a lower case. This makes it easier for people to spot whether something in the code is referring to a class or a variable,
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check the formatting of the code. You have fields starting with CapitalLetters (should be capitalLetters for a field), which can under certain circumstances cause logic errors or even compiler errors.
You have insufficient spacing between tokens and your lines are too long. I have broken the long lines so you can see how to do it.
Sorting out those thing swill make your code easier to understand and make it easier to read and will get you better marks
 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont want to declare key and values as an object




instead i want to use scanner to input the values into hashmap...how to do it?
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your requirements are a bit vague and it's hard to know what you're really shooting for, but here's a stab at it.


 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Caren the code you mentioned isn't working ...m getting an error: " getValue() method doesn't exist " ...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Your requirements are a bit vague and it's hard to know what you're really shooting for, but here's a stab at it...

I like your method of getting the data - ie, an array of field names - but not your final structure. You might want to read the StringsAreBad page to find out why.

Personally, I'd say there are at least two types in play here: a Student, and a User. Now in many cases they may refer to the same person, but that doesn't necessarily mean they're the same thing. A User could possibly also be a teacher, or an admin; and I doubt that a Student can't exist without being a User of the system.

Just a few of the things you need to consider when thinking about types (or classes).

HIH

Winston
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Carey Brown wrote:Your requirements are a bit vague and it's hard to know what you're really shooting for, but here's a stab at it...

I like your method of getting the data - ie, an array of field names - but not your final structure. You might want to read the StringsAreBad page to find out why.

Personally, I'd say there are at least two types in play here: a Student, and a User. Now in many cases they may refer to the same person, but that doesn't necessarily mean they're the same thing. A User could possibly also be a teacher, or an admin; and I doubt that a Student can't exist without being a User of the system.

Just a few of the things you need to consider when thinking about types (or classes).

HIH

Winston


Sorry, getValue() should have been get(). I just threw it together with notepad and didn't check it. I know strings are not a good way to add structure but it seemed like that was what the OP was looking for as other suggestions were not what he wanted.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!