• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How do I put the same hashmap with different values for each loop to my Hashmap<String, Hashmap>

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am creating a student database that
- Lets user create new student records
- Lets user find a student record by their student ID
- if the student is found, output their details
This is the code I made:

After the first loop, if I choose to create a new student record it will to start to overwrite the previous one and I get two hashmaps with the same values inside my Hashmap<String, Hashmap<String, String>>:  

How do I create new values for the same hashmap without overwriting the previous inputted hashmaps? I am not supposed any methods, classes or anything object related.
 
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch and enjoy the stay.

Well, think about the design first. What I'd expect perhaps as a user of your API is something like this:

where

When you create Student class, read about equals() and hashCode() and when they are needed and decide whether you need to override those methods in your Student class.

This is one way of structuring your system.
 
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

Liutauras Vilda wrote:Welcome to the Ranch and enjoy the stay. -Thanks


We have not really covered classes,methods or objects yet. Our professor evaluates us based on how we use what was taught in class.
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaredson Fetiza wrote:We have not really covered classes,methods or objects yet. Our professor evaluates us based on how we use what was taught in class.


I see. Ok then. Please start by letting us know, what tools do we have at hand?

Jaredson Fetiza wrote:I am not supposed any methods, classes or anything object related


That might be very tricky or even impossible.
 
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
We just covered:
- Arrays
- Arraylists
- For each loop
- Multidimensional Arrays
- Arraylist
- Hashset
- Hashmap

Are there no hashmap methods I can use to somehow do I want after line 24?
 
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
This is the instructions for the task if it helps.
Create a student database program that stores student records, which consist of:

Student ID
Full name
Age
Gender
Course
Year enrolled
The user will be asked whether they will enter a new student record or to search for a student record.

If they will search for a student then ask them for the student ID. If the student ID is found output the details of the student, if not then output not found.

If they will enter a new student record then allow the user to enter the students new details.

The program must also keep looping until the user inputs "quit".
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you can't create any custom classes like Student, but you can use Map data structure, then ok, your approach may work having:


Jaredson Fetiza wrote:How do I create new values for the same hashmap without overwriting the previous inputted hashmaps?


Overriding happens when you have the key you are about to add already added to a map.

For instance
 
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

Overriding happens when you have the key you are about to add already added to a map.

For instance



Yeah but I cant create new keys and value with my approach (not that I know of) because it is a loop using the same hashmap. I am experimenting with the hashmaps methods rn like
clone(), keyset(), clear() or values to somehow see if I can get around that.
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaredson Fetiza wrote:Yeah but I cant create new keys and value with my approach (not that I know of) because it is a loop using the same hashmap.


Why? Could you give an example please.

Jaredson Fetiza wrote:I am experimenting with the hashmaps methods rn like clone(), keyset(), clear() or values to somehow see if I can get around that.


I don't think you need any of those yet.
 
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

Liutauras Vilda wrote:Why? Could you give an example please.


Because I need to create a new one each time it loops?
- like this

Am I doing it right? What did you have in mind?
Honestly I am willing to start over from scratch cause I feel like it shouldn't be this complicated and there is a simpler approach of doing this haha
 
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaredson Fetiza wrote:. . . We have not really covered classes,methods or objects yet. . . .

What sort of course is 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

Campbell Ritchie wrote:What sort of course is this?


Computer Science?
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaredson Fetiza wrote:

Liutauras Vilda wrote:Why? Could you give an example please.


Because I need to create a new one each time it loops?
- like this

Am I doing it right? What did you have in mind?
Honestly I am willing to start over from scratch cause I feel like it shouldn't be this complicated and there is a simpler approach of doing this haha


So what I was thinking of (and that is purely to work on the structure you laid down with Map<String, Map<String, String>>):

But again, I'm not entirely sure what you can use in your assignment. It doesn't say that you are not allowed something, nor it does say what you are allowed. Only you said you are not allowed that and that. So I'm not sure.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaredson Fetiza wrote:. . . Computer Science?

What you said makes me wonder whether you been taught any object‑oriented programming. I presume you have attended all the teaching.
 
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

Liutauras Vilda wrote:
So what I was thinking of (and that is purely to work on the structure you laid down with Map<String, Map<String, String>>):

But again, I'm not entirely sure what you can use in your assignment. It doesn't say that you are not allowed something, nor it does say what you are allowed. Only you said you are not allowed that and that. So I'm not sure.



Wait I don't get it. Isn't this what I am trying to do? Except that the main problem is I cant do what's in your comment.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . Overriding . . .

There are several meaning for the word “overriding”. It is commonly used about methods in object‑oriented programming, but Liutauras is using it differently. If you look in the Java™ Tutorials, you will see it says a Map “models the mathematical function abstraction”. Your map constitutes a function where x ↦ y and you are overriding a mapping in the function so that x ↦ z.
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaredson Fetiza wrote:Wait I don't get it. Isn't this what I am trying to do? Except that the main problem is I cant do what's in your comment.


I have 2 Maps defined.
You have 2 Maps defined.

One of my Maps is defined outside loop, another Map inside the loop.
How about yours?
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaredson Fetiza wrote:. . . Isn't this what I am trying to do? . . .

I think the above is dreadful design, but it should work. Lines 7‑12 create a Map as an anonymous class; the calls to put(...); should populate the map with all the data you require, so use as many put(); calls, or as few, as you need.
The types in lines 4, 5, and 7 can (should?) all be replaced by var.
 
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
I see so I cant use the same key because it can only contain one value. I thought I was creating a new key with the same name. If I need to create new keys each it loops,is it even possible to loop student record creation using hashmap?
I will try to start from scratch and think of something.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the same key as often as you like, but you will overwrite the value and thereby change the pair and change the function. If you have multiple mappings, you use a different key for each pair. Then each put() call changes the function by making it larger (=enlarging its domain) rather than altering a pair.
Remember that in a function, x ↦ y means that every time you put x into the function, you get y, never a, b, c, or z.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Half an hour ago, I wrote:You can use the same key as often as you like . . .

If you use the same value repeatedly, you will have every key pointing to that value. Adding pairs to it will change  the state of the value, but not which object corresponds to y above.
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:overwrite


Thank you for correcting me. I wasn't precise earlier when I was choosing words.
 
Bartender
Posts: 10979
87
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

Jaredson Fetiza wrote:This is the instructions for the task if it helps.
Create a student database program that stores student records, which consist of:
Student ID
Full name
Age
Gender
Course
Year enrolled


This is EXACTLY the way the design for a Student object would be specified. Any other way to deal with that would be tortuous and send me off into the woods screaming. I'm sorry, but with wording like this in the instructions I believe they are telling you to create a Student object.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . a Student object . . . Any other way . . . would . .  send me off into the woods screaming. . . . they are telling you to create a Student object.

Or they would if they actually taught tobject‑oriented programming. I had the good fortune to be taught by somebody (Elizabeth Norvall) who actually taught object‑orientation.
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic