Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

HashMaps

 
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?
 
Rancher
Posts: 4625
47
  • 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,
 
Marshal
Posts: 69894
278
  • 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?
 
Bartender
Posts: 7208
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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 " ...
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
Bartender
Posts: 7208
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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.
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic