Hashmap and finding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    #1

    Hashmap and finding

    When I run choice == 2 I'm suppose to a an out.println back finding a product.
    but I get Null for all the values.
    Can someone help me with this.

    Code:
    class Computer_listing {
    
    	private String id;
    
    	private String name;
    
    	private int harddrive;
    
    	private int memory;
    
    	private float speed;
    
    	public Computer_listing(String idx) {
    
             	this.id = idx;
    
    	}
    
    
    	public Computer_listing(String cd, String cn, int hd, int me,
    			float sp) {
    		this.id = cd;
    		this.name = cn;
    		this.harddrive = hd;
    		this.memory = me;
    		this.speed = sp;
    
    	}
    
    	public String getId() {
    
    		return this.id;
    
    	}
    
    	public String getName() {
    
    		return this.name;
    
    	}
    
    	public int getHardrive() {
    
    		return this.harddrive;
    
    	}
    
    	public int getMemory() {
    
    		return this.memory;
    
    	}
    
    	public float getSpeed() {
    
    		return this.speed;
    
    	}
    
    	public void setId(String cd) {
    
    		this.id = cd;
    	}
    
    	public void setName(String cn) {
    
            	this.name = cn;
    
    	}
    
    	public void setHarddrive(Integer hd) {
    
    		this.harddrive = hd;
    
    	}
    
    	public void setMemory(Integer me) {
    
    		this.memory = me;
    
    	}
    
    	public void setSpeed(float sp) {
    
    		this.speed = sp;
    
    	}
    }
    
    
    public class ComputerInvent{
    
    
      public static void main(String s[]){
    
             HashMap<String,Computer_listing> hs = new HashMap<String,Computer_listing>();
             Computer_listing[] computer = new Computer_listing[2];
             computer[0] = new Computer_listing("IBM123", "IBM ThinkCentera",2, 5000, 340);
             computer[1] = new Computer_listing("Apple", "15 PowerBook", 2, 5000, 340);
             Scanner kbd = new Scanner(System.in);
    			int choice;
    			System.out.println("Make a Section: ");
    			System.out.println("1. Enter Info ");
    			System.out.println("2. Print all ");
    			System.out.println("3. Exit ");
    			System.out.print("\nPlease press Enter afer each response");
    			System.out.println("Enter your chose please: ");
    			choice = kbd.nextInt();
    			kbd.nextLine();
    			
    			if (choice == 2) {
    				 Collection<Computer_listing> values = hs.values();
    
    		         Scanner kbd2 = new Scanner(System.in);
    					int choice2;//new section making
    					System.out.println("Make a Section: ");
    					System.out.println("1. Print all ");
    					System.out.println("2. Exit ");
    					System.out.print("\nPlease press Enter afer each response");
    					System.out.println("Enter your chose please: ");
    					choice2 = kbd.nextInt();
    					kbd2.nextLine();
    					if (choice2 == 1) {
    						for (Computer_listing temp : values) {
    				        	System.out.println("Item is enter in the db\n");
    				        	System.out.printf("Id: %s \n",temp.getId());
    				        	System.out.printf("Product name: %s \n",temp.getName());
    				        	System.out.printf("Product name: %s \n",temp.getHardrive());
    				        	System.out.printf("Product name: %s \n",temp.getMemory());
    				        	System.out.printf("Product name: %s \n",temp.getSpeed());
    				        	System.out.println("--------------------");
    						}
    
    		 }// close the choice2==1
    					if (choice2 == 2) {
    						System.out.printf("Good bye");
    					 }// close the choice2==2
    
    		if (choice == 3) {
    			System.out.printf("Good bye");
    		}// close the choice == 3
    
    		}//close else
    
    
      }//close main
    
    
    }//close class

    thanks
    Nomad
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by nomad
    When I run choice == 2 I'm suppose to a an out.println back finding a product.
    but I get Null for all the values.
    Can someone help me with this.

    Code:
    class Computer_listing {
     
    	private String id;
     
    	private String name;
     
    	private int harddrive;
     
    	private int memory;
     
    	private float speed;
     
    	public Computer_listing(String idx) {
     
    	this.id = idx;
     
    	}
     
     
    	public Computer_listing(String cd, String cn, int hd, int me,
    			float sp) {
    		this.id = cd;
    		this.name = cn;
    		this.harddrive = hd;
    		this.memory = me;
    		this.speed = sp;
     
    	}
     
    	public String getId() {
     
    		return this.id;
     
    	}
     
    	public String getName() {
     
    		return this.name;
     
    	}
     
    	public int getHardrive() {
     
    		return this.harddrive;
     
    	}
     
    	public int getMemory() {
     
    		return this.memory;
     
    	}
     
    	public float getSpeed() {
     
    		return this.speed;
     
    	}
     
    	public void setId(String cd) {
     
    		this.id = cd;
    	}
     
    	public void setName(String cn) {
     
    	this.name = cn;
     
    	}
     
    	public void setHarddrive(Integer hd) {
     
    		this.harddrive = hd;
     
    	}
     
    	public void setMemory(Integer me) {
     
    		this.memory = me;
     
    	}
     
    	public void setSpeed(float sp) {
     
    		this.speed = sp;
     
    	}
    }
     
     
    public class ComputerInvent{
     
     
    public static void main(String s[]){
     
    HashMap<String,Computer_listing> hs = new HashMap<String,Computer_listing>();
    Computer_listing[] computer = new Computer_listing[2];
    computer[0] = new Computer_listing("IBM123", "IBM ThinkCentera",2, 5000, 340);
    computer[1] = new Computer_listing("Apple", "15 PowerBook", 2, 5000, 340);
    Scanner kbd = new Scanner(System.in);
    			int choice;
    			System.out.println("Make a Section: ");
    			System.out.println("1. Enter Info ");
    			System.out.println("2. Print all ");
    			System.out.println("3. Exit ");
    			System.out.print("\nPlease press Enter afer each response");
    			System.out.println("Enter your chose please: ");
    			choice = kbd.nextInt();
    			kbd.nextLine();
     
    			if (choice == 2) {
    				 Collection<Computer_listing> values = hs.values();
     
    		 Scanner kbd2 = new Scanner(System.in);
    					int choice2;//new section making
    					System.out.println("Make a Section: ");
    					System.out.println("1. Print all ");
    					System.out.println("2. Exit ");
    					System.out.print("\nPlease press Enter afer each response");
    					System.out.println("Enter your chose please: ");
    					choice2 = kbd.nextInt();
    					kbd2.nextLine();
    					if (choice2 == 1) {
    						for (Computer_listing temp : values) {
    					 System.out.println("Item is enter in the db\n");
    					 System.out.printf("Id: %s \n",temp.getId());
    					 System.out.printf("Product name: %s \n",temp.getName());
    					 System.out.printf("Product name: %s \n",temp.getHardrive());
    					 System.out.printf("Product name: %s \n",temp.getMemory());
    					 System.out.printf("Product name: %s \n",temp.getSpeed());
    					 System.out.println("--------------------");
    						}
     
    		 }// close the choice2==1
    					if (choice2 == 2) {
    						System.out.printf("Good bye");
    					 }// close the choice2==2
     
    		if (choice == 3) {
    			System.out.printf("Good bye");
    		}// close the choice == 3
     
    		}//close else
     
     
    }//close main
     
     
    }//close class

    thanks
    Nomad
    Where did you let the user enter the info?
    When you do
    Code:
     
    Collection<Computer_listing> values = hs.values();
    make sure there was something put in hs first.

    Comment

    • nomad
      Recognized Expert Contributor
      • Mar 2007
      • 664

      #3
      Originally posted by r035198x
      Where did you let the user enter the info?
      When you do
      Code:
       
      Collection<Computer_listing> values = hs.values();
      make sure there was something put in hs first.

      here is the input section for the user...
      Same Class...

      Code:
        public static void main(String s[]){
      
               HashMap<String,Computer_listing> hs = new HashMap<String,Computer_listing>();
               Computer_listing[] computer = new Computer_listing[2];
               computer[0] = new Computer_listing("IBM123", "IBM ThinkCentera",2, 5000, 340);
               computer[1] = new Computer_listing("Apple", "15 PowerBook", 2, 5000, 340);
              
               
               Scanner kbd = new Scanner(System.in);
      			int choice;
      			System.out.println("Make a Section: ");
      			System.out.println("1. Enter Info ");
      			System.out.println("2. Print all ");
      			System.out.println("3. Exit ");
      			System.out.print("\nPlease press Enter afer each response");
      			System.out.println("Enter your chose please: ");
      			choice = kbd.nextInt();
      			kbd.nextLine();
      			if (choice == 1) {
               Scanner ifput = new Scanner(System.in);
               ifput.useDelimiter("\r\n");
      
                 while(true){
      
                     String msg = null;
                     Computer_listing compdb = null;
      
                      System.out.println("Enter <ComputerID> ie IBM123A - Type <Stop> to print");
                 	msg = ifput.next();
      
                      if (msg.equalsIgnoreCase("STOP"))
      
      		  break;
      
                  	else
      		  compdb = new Computer_listing(msg);
      
                      System.out.println("Enter Computer Name:");
      		compdb.setName(ifput.next());
      
      		System.out.println("Enter HardDrive: ie 12");
      		compdb.setHarddrive(Integer.parseInt(ifput.next()));
      
      
      		System.out.println("Enter Memory: ie 7865");
      		compdb.setMemory(Integer.parseInt(ifput.next()));
      
      		System.out.println("Enter Clock Speed: ie 765.00");
      		compdb.setSpeed(Float.parseFloat(ifput.next()));
      
                      hs.put(msg,compdb);
      
                }
      
                System.out.println("<Done>");
      
                Collection<Computer_listing> values = hs.values();
                int i = 0;
      
                for (Computer_listing temp : values) {
              	System.out.println("Item is enter in the db\n");
              	System.out.printf("Product Id: %s \n",temp.getId());
              	System.out.printf("Product Name: %s \n",temp.getName());
              	System.out.printf("Product Hardrive: %s \n",temp.getHardrive());
              	System.out.printf("Product Memory: %s \n",temp.getMemory());
              	System.out.printf("Product Speed: %s \n",temp.getSpeed());
              	System.out.println("--------------------");
      			i++;
      
                }
      			} // close the if loop
      nomad

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by nomad
        here is the input section for the user...
        Same Class...

        Code:
         public static void main(String s[]){
         
        HashMap<String,Computer_listing> hs = new HashMap<String,Computer_listing>();
        Computer_listing[] computer = new Computer_listing[2];
        computer[0] = new Computer_listing("IBM123", "IBM ThinkCentera",2, 5000, 340);
        computer[1] = new Computer_listing("Apple", "15 PowerBook", 2, 5000, 340);
         
         
        Scanner kbd = new Scanner(System.in);
        			int choice;
        			System.out.println("Make a Section: ");
        			System.out.println("1. Enter Info ");
        			System.out.println("2. Print all ");
        			System.out.println("3. Exit ");
        			System.out.print("\nPlease press Enter afer each response");
        			System.out.println("Enter your chose please: ");
        			choice = kbd.nextInt();
        			kbd.nextLine();
        			if (choice == 1) {
        Scanner ifput = new Scanner(System.in);
        ifput.useDelimiter("\r\n");
         
        while(true){
         
        String msg = null;
        Computer_listing compdb = null;
         
        System.out.println("Enter <ComputerID> ie IBM123A - Type <Stop> to print");
        	msg = ifput.next();
         
        if (msg.equalsIgnoreCase("STOP"))
         
        		 break;
         
        	else
        		 compdb = new Computer_listing(msg);
         
        System.out.println("Enter Computer Name:");
        		compdb.setName(ifput.next());
         
        		System.out.println("Enter HardDrive: ie 12");
        		compdb.setHarddrive(Integer.parseInt(ifput.next()));
         
         
        		System.out.println("Enter Memory: ie 7865");
        		compdb.setMemory(Integer.parseInt(ifput.next()));
         
        		System.out.println("Enter Clock Speed: ie 765.00");
        		compdb.setSpeed(Float.parseFloat(ifput.next()));
         
        hs.put(msg,compdb);
         
        }
         
        System.out.println("<Done>");
         
        Collection<Computer_listing> values = hs.values();
        int i = 0;
         
        for (Computer_listing temp : values) {
        	System.out.println("Item is enter in the db\n");
        	System.out.printf("Product Id: %s \n",temp.getId());
        	System.out.printf("Product Name: %s \n",temp.getName());
        	System.out.printf("Product Hardrive: %s \n",temp.getHardrive());
        	System.out.printf("Product Memory: %s \n",temp.getMemory());
        	System.out.printf("Product Speed: %s \n",temp.getSpeed());
        	System.out.println("--------------------");
        			i++;
         
        }
        			} // close the if loop
        nomad
        In
        Code:
        compdb = new Computer_listing(msg);
        are you creating a Computer_listin g object using only one String as parameter?
        Where is the constructor that takes that?

        Comment

        Working...