0

when i put different key into hashmap after that i use get(key), different key shows the same result, i have no idea why this happen

I have two class, Room class and Office class

This is my Room Class

import java.util.HashMap;
import java.util.Scanner;

public class Room {
private static int roomNumber;
private static String description;

public int getRoomNum(){return roomNumber;}
public String getDescription(){return description;}
public void setDescription(String description){
    this.description=description;
}
public void setRoomNum(int roomNumber){
    this.roomNumber=roomNumber;
}

public Scanner sc = new Scanner(System.in);
public static HashMap<Integer, Room> room = new HashMap<>();

public void askData(){
    System.out.println("ENTER ROOM NUMBER:");
    this.roomNumber=sc.nextInt(); 
    sc.nextLine();
    System.out.println("Enter description:");
    this.description = sc.nextLine();
}
public String toString(){
    return this.getRoomNum()+": "+this.getDescription();
}
public static void main(String[] args) {
Room r = new Office();
    r.room.put(1,new Office(1,"java","asdfw"));
r.room.put(2,new Office(2,"jafasdfva","ghasdfasow"));

//----------------------My problem is here

    Room s = r.room.get(1);
    Room s2 = r.room.get(2);
    System.out.println(s.getRoomNum()+" . "+s.getDescription());
    System.out.println(s2.getRoomNum()+" . "+s2.getDescription());

 for(Integer keey: room.keySet()){
    System.out.println(r.room.get(roomNumber));

 }

}

// following are the result

//2 . jafasdfva

//2 . jafasdfva

//2: jafasdfva. ghasdfasow

//2: jafasdfva. ghasdfasow

//-------------------------------------

}
}

i have int key 1 and 2, but when i get key 1 and 2, result will show key 2 two times, i dont know why this happend

//------------------------------------

This is my Office class

import java.util.ArrayList;
import java.util.HashMap;

public class Office extends Room {
private String staffMembers;

public String enter,check;

public Office(int roomNumber,String description, String staffMembers){
    this.setRoomNum(roomNumber);
    this.setDescription(description);
    this.staffMembers=staffMembers;
}

public Office(){}
public void askData(){
    super.askData();
    StringBuilder sb = new StringBuilder();

    do{
        System.out.println("Enter members in this room: ");
        enter =sc.nextLine();
        sb.append(enter+", ");

        System.out.println("Enter Y to continule.");
        check = sc.nextLine();
    }while(check.equalsIgnoreCase("Y"));

    this.staffMembers=sb+"";

    this.room.put(this.getRoomNum(), new Office(this.getRoomNum(),this.getDescription(),staffMembers));

}

public String toString(){

    return this.getRoomNum()+": "+this.getDescription()+". "+
    this.staffMembers; 
}

}
J. Doe
  • 11
  • 4
  • The code below "My problem is here" wouldn't compile, what are you doing exactly? – DPM Sep 01 '17 at 22:55
  • @shmosel It would help if you explain why this is a duplicate. I see it but it's really not obvious. – lexicore Sep 01 '17 at 22:58
  • @lexicore I don't see why not. There are two answers there; the first directly addresses this scenario. – shmosel Sep 01 '17 at 23:00
  • @shmosel I don't object the duplicate, you're correct about it. I just don't think it is obvious to the OP. – lexicore Sep 01 '17 at 23:03
  • @lexicore You're free to help him out. – shmosel Sep 01 '17 at 23:05
  • The cold near "My problem is here" i changed , is like i put 1 and 2 as key, but when i use get(1) and get(2), the answer is the same, why this happen? – J. Doe Sep 01 '17 at 23:06
  • can you give me some help – J. Doe Sep 01 '17 at 23:06
  • 3
    @J.Doe Your fields are static. So basically all `Room`s share have the same `roomNumber` and `description` - the ones set by the last constructor call. – lexicore Sep 01 '17 at 23:08
  • @lexicore you are right, damn i spent more than 1 h didnt find this, but if i delete static from roomNumber, i can not use it as key word, because its not static, i want to use for each loop to display hashmap, do you know how to get key from keyset(), i mean if x.keySet()=[1,2] , how can i get key 1 and 2? waiting for your reply thanks a lot – J. Doe Sep 01 '17 at 23:26
  • @J.Doe Sorry, I do not have resources to guide you through this. – lexicore Sep 01 '17 at 23:27
  • @lexicore for(Entry entry: map.entrySet()) { // get key K key = entry.getKey(); // get value V value = entry.getValue(); } – J. Doe Sep 01 '17 at 23:34
  • @ i find the answer, i should learn more before the exercise, it took too much time x( sleep lol – J. Doe Sep 01 '17 at 23:35

0 Answers0