• 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

HashMaps

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Caren the code you mentioned isn't working ...m getting an error: " getValue() method doesn't exist " ...
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A feeble attempt to tell you about our stuff that makes us money
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic