Hack Forums
[Java] Hashmap saves only the last entry i type. [JAVA] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Hashmap saves only the last entry i type. [JAVA] (/showthread.php?tid=5780472)



Hashmap saves only the last entry i type. [JAVA] - georgeziozas - Jan 6, 2018

hey there programers, 

I am making a very simple graphical booking system and im facing a problem with the hashmap.

So what im trying to do is that the user enters some information at textboxes and i save them in a hashmap.
The problem is that when i print that hashmap at the end of the program it only shows me the last entry of the user and not the previous ones.

Here is the code. [the code is a bit rough atm , maybe is a litle hard to read]

Code
public class GUI extends Application {
   static HashMap<String, String> RoomsMap = new HashMap(); //dimiourgia hashmap wste na peraso mesa oles tis kratiseis
    TextField textField1 = new TextField ();
    TextField textField2 = new TextField ();
    TextField textField3 = new TextField ();
    TextField textField4 = new TextField ();
    TextField textField5 = new TextField ();

void FillHashMapRooms() { //gemizei ta hashmap simfona me ta stoixeia p edose o xristis
       RoomsMap.put("Eidos-Domatiou ",textField1.getText());
       RoomsMap.put("Domatio-Name ",textField2.getText());
       RoomsMap.put("Domatio-Code ",textField3.getText());
       RoomsMap.put("Domatio-Price ",textField5.getText());
       RoomsMap.put("Domatio-Date ", textField4.getText());
   }

b18 = new Button();
       b18.setText("CONFIRM & ADD");
       b18.setOnAction(new EventHandler<ActionEvent>() {
       
           @Override public void handle(ActionEvent e) {
              FillHashMapRooms();
              window.setScene(s1);
       }});


these are the parts of the code that have to do with my problem.
b18 is the button that the user press when he has placed his information.

for any further info, let me know.
Thanks in advance fellas.


RE: Hashmap saves only the last entry i type. [JAVA] - XMR - Jan 6, 2018

When you store the new user entered values in the HashMap, the older ones are overwritten.


RE: Hashmap saves only the last entry i type. [JAVA] - georgeziozas - Jan 6, 2018

(Jan 6, 2018 11:03 AM)Arccotangent Wrote:  When you store the new user entered values in the HashMap, the older ones are overwritten.

yes i realize that.
Thats my problem,  do you know how i can make it work and save all the values?


well i just found the problem.
its a stupid one by me.
I provide the same key every time so the hashmap overwrites it.