473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Hashmap and finding

nomad
664 Expert 512MB
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.

Expand|Select|Wrap|Line Numbers
  1. class Computer_listing {
  2.  
  3.     private String id;
  4.  
  5.     private String name;
  6.  
  7.     private int harddrive;
  8.  
  9.     private int memory;
  10.  
  11.     private float speed;
  12.  
  13.     public Computer_listing(String idx) {
  14.  
  15.              this.id = idx;
  16.  
  17.     }
  18.  
  19.  
  20.     public Computer_listing(String cd, String cn, int hd, int me,
  21.             float sp) {
  22.         this.id = cd;
  23.         this.name = cn;
  24.         this.harddrive = hd;
  25.         this.memory = me;
  26.         this.speed = sp;
  27.  
  28.     }
  29.  
  30.     public String getId() {
  31.  
  32.         return this.id;
  33.  
  34.     }
  35.  
  36.     public String getName() {
  37.  
  38.         return this.name;
  39.  
  40.     }
  41.  
  42.     public int getHardrive() {
  43.  
  44.         return this.harddrive;
  45.  
  46.     }
  47.  
  48.     public int getMemory() {
  49.  
  50.         return this.memory;
  51.  
  52.     }
  53.  
  54.     public float getSpeed() {
  55.  
  56.         return this.speed;
  57.  
  58.     }
  59.  
  60.     public void setId(String cd) {
  61.  
  62.         this.id = cd;
  63.     }
  64.  
  65.     public void setName(String cn) {
  66.  
  67.             this.name = cn;
  68.  
  69.     }
  70.  
  71.     public void setHarddrive(Integer hd) {
  72.  
  73.         this.harddrive = hd;
  74.  
  75.     }
  76.  
  77.     public void setMemory(Integer me) {
  78.  
  79.         this.memory = me;
  80.  
  81.     }
  82.  
  83.     public void setSpeed(float sp) {
  84.  
  85.         this.speed = sp;
  86.  
  87.     }
  88. }
  89.  
  90.  
  91. public class ComputerInvent{
  92.  
  93.  
  94.   public static void main(String s[]){
  95.  
  96.          HashMap<String,Computer_listing> hs = new HashMap<String,Computer_listing>();
  97.          Computer_listing[] computer = new Computer_listing[2];
  98.          computer[0] = new Computer_listing("IBM123", "IBM ThinkCentera",2, 5000, 340);
  99.          computer[1] = new Computer_listing("Apple", "15 PowerBook", 2, 5000, 340);
  100.          Scanner kbd = new Scanner(System.in);
  101.             int choice;
  102.             System.out.println("Make a Section: ");
  103.             System.out.println("1. Enter Info ");
  104.             System.out.println("2. Print all ");
  105.             System.out.println("3. Exit ");
  106.             System.out.print("\nPlease press Enter afer each response");
  107.             System.out.println("Enter your chose please: ");
  108.             choice = kbd.nextInt();
  109.             kbd.nextLine();
  110.  
  111.             if (choice == 2) {
  112.                  Collection<Computer_listing> values = hs.values();
  113.  
  114.                  Scanner kbd2 = new Scanner(System.in);
  115.                     int choice2;//new section making
  116.                     System.out.println("Make a Section: ");
  117.                     System.out.println("1. Print all ");
  118.                     System.out.println("2. Exit ");
  119.                     System.out.print("\nPlease press Enter afer each response");
  120.                     System.out.println("Enter your chose please: ");
  121.                     choice2 = kbd.nextInt();
  122.                     kbd2.nextLine();
  123.                     if (choice2 == 1) {
  124.                         for (Computer_listing temp : values) {
  125.                             System.out.println("Item is enter in the db\n");
  126.                             System.out.printf("Id: %s \n",temp.getId());
  127.                             System.out.printf("Product name: %s \n",temp.getName());
  128.                             System.out.printf("Product name: %s \n",temp.getHardrive());
  129.                             System.out.printf("Product name: %s \n",temp.getMemory());
  130.                             System.out.printf("Product name: %s \n",temp.getSpeed());
  131.                             System.out.println("--------------------");
  132.                         }
  133.  
  134.          }// close the choice2==1
  135.                     if (choice2 == 2) {
  136.                         System.out.printf("Good bye");
  137.                      }// close the choice2==2
  138.  
  139.         if (choice == 3) {
  140.             System.out.printf("Good bye");
  141.         }// close the choice == 3
  142.  
  143.         }//close else
  144.  
  145.  
  146.   }//close main
  147.  
  148.  
  149. }//close class

thanks
Nomad
May 7 '07 #1
3 1753
r035198x
13,262 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. class Computer_listing {
  2.  
  3.     private String id;
  4.  
  5.     private String name;
  6.  
  7.     private int harddrive;
  8.  
  9.     private int memory;
  10.  
  11.     private float speed;
  12.  
  13.     public Computer_listing(String idx) {
  14.  
  15.     this.id = idx;
  16.  
  17.     }
  18.  
  19.  
  20.     public Computer_listing(String cd, String cn, int hd, int me,
  21.             float sp) {
  22.         this.id = cd;
  23.         this.name = cn;
  24.         this.harddrive = hd;
  25.         this.memory = me;
  26.         this.speed = sp;
  27.  
  28.     }
  29.  
  30.     public String getId() {
  31.  
  32.         return this.id;
  33.  
  34.     }
  35.  
  36.     public String getName() {
  37.  
  38.         return this.name;
  39.  
  40.     }
  41.  
  42.     public int getHardrive() {
  43.  
  44.         return this.harddrive;
  45.  
  46.     }
  47.  
  48.     public int getMemory() {
  49.  
  50.         return this.memory;
  51.  
  52.     }
  53.  
  54.     public float getSpeed() {
  55.  
  56.         return this.speed;
  57.  
  58.     }
  59.  
  60.     public void setId(String cd) {
  61.  
  62.         this.id = cd;
  63.     }
  64.  
  65.     public void setName(String cn) {
  66.  
  67.     this.name = cn;
  68.  
  69.     }
  70.  
  71.     public void setHarddrive(Integer hd) {
  72.  
  73.         this.harddrive = hd;
  74.  
  75.     }
  76.  
  77.     public void setMemory(Integer me) {
  78.  
  79.         this.memory = me;
  80.  
  81.     }
  82.  
  83.     public void setSpeed(float sp) {
  84.  
  85.         this.speed = sp;
  86.  
  87.     }
  88. }
  89.  
  90.  
  91. public class ComputerInvent{
  92.  
  93.  
  94. public static void main(String s[]){
  95.  
  96. HashMap<String,Computer_listing> hs = new HashMap<String,Computer_listing>();
  97. Computer_listing[] computer = new Computer_listing[2];
  98. computer[0] = new Computer_listing("IBM123", "IBM ThinkCentera",2, 5000, 340);
  99. computer[1] = new Computer_listing("Apple", "15 PowerBook", 2, 5000, 340);
  100. Scanner kbd = new Scanner(System.in);
  101.             int choice;
  102.             System.out.println("Make a Section: ");
  103.             System.out.println("1. Enter Info ");
  104.             System.out.println("2. Print all ");
  105.             System.out.println("3. Exit ");
  106.             System.out.print("\nPlease press Enter afer each response");
  107.             System.out.println("Enter your chose please: ");
  108.             choice = kbd.nextInt();
  109.             kbd.nextLine();
  110.  
  111.             if (choice == 2) {
  112.                  Collection<Computer_listing> values = hs.values();
  113.  
  114.          Scanner kbd2 = new Scanner(System.in);
  115.                     int choice2;//new section making
  116.                     System.out.println("Make a Section: ");
  117.                     System.out.println("1. Print all ");
  118.                     System.out.println("2. Exit ");
  119.                     System.out.print("\nPlease press Enter afer each response");
  120.                     System.out.println("Enter your chose please: ");
  121.                     choice2 = kbd.nextInt();
  122.                     kbd2.nextLine();
  123.                     if (choice2 == 1) {
  124.                         for (Computer_listing temp : values) {
  125.                      System.out.println("Item is enter in the db\n");
  126.                      System.out.printf("Id: %s \n",temp.getId());
  127.                      System.out.printf("Product name: %s \n",temp.getName());
  128.                      System.out.printf("Product name: %s \n",temp.getHardrive());
  129.                      System.out.printf("Product name: %s \n",temp.getMemory());
  130.                      System.out.printf("Product name: %s \n",temp.getSpeed());
  131.                      System.out.println("--------------------");
  132.                         }
  133.  
  134.          }// close the choice2==1
  135.                     if (choice2 == 2) {
  136.                         System.out.printf("Good bye");
  137.                      }// close the choice2==2
  138.  
  139.         if (choice == 3) {
  140.             System.out.printf("Good bye");
  141.         }// close the choice == 3
  142.  
  143.         }//close else
  144.  
  145.  
  146. }//close main
  147.  
  148.  
  149. }//close class

thanks
Nomad
Where did you let the user enter the info?
When you do
Expand|Select|Wrap|Line Numbers
  1.  
  2. Collection<Computer_listing> values = hs.values();
  3.  
make sure there was something put in hs first.
May 7 '07 #2
nomad
664 Expert 512MB
Where did you let the user enter the info?
When you do
Expand|Select|Wrap|Line Numbers
  1.  
  2. Collection<Computer_listing> values = hs.values();
  3.  
make sure there was something put in hs first.

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

Expand|Select|Wrap|Line Numbers
  1.   public static void main(String s[]){
  2.  
  3.          HashMap<String,Computer_listing> hs = new HashMap<String,Computer_listing>();
  4.          Computer_listing[] computer = new Computer_listing[2];
  5.          computer[0] = new Computer_listing("IBM123", "IBM ThinkCentera",2, 5000, 340);
  6.          computer[1] = new Computer_listing("Apple", "15 PowerBook", 2, 5000, 340);
  7.  
  8.  
  9.          Scanner kbd = new Scanner(System.in);
  10.             int choice;
  11.             System.out.println("Make a Section: ");
  12.             System.out.println("1. Enter Info ");
  13.             System.out.println("2. Print all ");
  14.             System.out.println("3. Exit ");
  15.             System.out.print("\nPlease press Enter afer each response");
  16.             System.out.println("Enter your chose please: ");
  17.             choice = kbd.nextInt();
  18.             kbd.nextLine();
  19.             if (choice == 1) {
  20.          Scanner ifput = new Scanner(System.in);
  21.          ifput.useDelimiter("\r\n");
  22.  
  23.            while(true){
  24.  
  25.                String msg = null;
  26.                Computer_listing compdb = null;
  27.  
  28.                 System.out.println("Enter <ComputerID> ie IBM123A - Type <Stop> to print");
  29.                msg = ifput.next();
  30.  
  31.                 if (msg.equalsIgnoreCase("STOP"))
  32.  
  33.           break;
  34.  
  35.                 else
  36.           compdb = new Computer_listing(msg);
  37.  
  38.                 System.out.println("Enter Computer Name:");
  39.         compdb.setName(ifput.next());
  40.  
  41.         System.out.println("Enter HardDrive: ie 12");
  42.         compdb.setHarddrive(Integer.parseInt(ifput.next()));
  43.  
  44.  
  45.         System.out.println("Enter Memory: ie 7865");
  46.         compdb.setMemory(Integer.parseInt(ifput.next()));
  47.  
  48.         System.out.println("Enter Clock Speed: ie 765.00");
  49.         compdb.setSpeed(Float.parseFloat(ifput.next()));
  50.  
  51.                 hs.put(msg,compdb);
  52.  
  53.           }
  54.  
  55.           System.out.println("<Done>");
  56.  
  57.           Collection<Computer_listing> values = hs.values();
  58.           int i = 0;
  59.  
  60.           for (Computer_listing temp : values) {
  61.             System.out.println("Item is enter in the db\n");
  62.             System.out.printf("Product Id: %s \n",temp.getId());
  63.             System.out.printf("Product Name: %s \n",temp.getName());
  64.             System.out.printf("Product Hardrive: %s \n",temp.getHardrive());
  65.             System.out.printf("Product Memory: %s \n",temp.getMemory());
  66.             System.out.printf("Product Speed: %s \n",temp.getSpeed());
  67.             System.out.println("--------------------");
  68.             i++;
  69.  
  70.           }
  71.             } // close the if loop
nomad
May 7 '07 #3
r035198x
13,262 8TB
here is the input section for the user...
Same Class...

Expand|Select|Wrap|Line Numbers
  1.  public static void main(String s[]){
  2.  
  3. HashMap<String,Computer_listing> hs = new HashMap<String,Computer_listing>();
  4. Computer_listing[] computer = new Computer_listing[2];
  5. computer[0] = new Computer_listing("IBM123", "IBM ThinkCentera",2, 5000, 340);
  6. computer[1] = new Computer_listing("Apple", "15 PowerBook", 2, 5000, 340);
  7.  
  8.  
  9. Scanner kbd = new Scanner(System.in);
  10.             int choice;
  11.             System.out.println("Make a Section: ");
  12.             System.out.println("1. Enter Info ");
  13.             System.out.println("2. Print all ");
  14.             System.out.println("3. Exit ");
  15.             System.out.print("\nPlease press Enter afer each response");
  16.             System.out.println("Enter your chose please: ");
  17.             choice = kbd.nextInt();
  18.             kbd.nextLine();
  19.             if (choice == 1) {
  20. Scanner ifput = new Scanner(System.in);
  21. ifput.useDelimiter("\r\n");
  22.  
  23. while(true){
  24.  
  25. String msg = null;
  26. Computer_listing compdb = null;
  27.  
  28. System.out.println("Enter <ComputerID> ie IBM123A - Type <Stop> to print");
  29.     msg = ifput.next();
  30.  
  31. if (msg.equalsIgnoreCase("STOP"))
  32.  
  33.          break;
  34.  
  35.     else
  36.          compdb = new Computer_listing(msg);
  37.  
  38. System.out.println("Enter Computer Name:");
  39.         compdb.setName(ifput.next());
  40.  
  41.         System.out.println("Enter HardDrive: ie 12");
  42.         compdb.setHarddrive(Integer.parseInt(ifput.next()));
  43.  
  44.  
  45.         System.out.println("Enter Memory: ie 7865");
  46.         compdb.setMemory(Integer.parseInt(ifput.next()));
  47.  
  48.         System.out.println("Enter Clock Speed: ie 765.00");
  49.         compdb.setSpeed(Float.parseFloat(ifput.next()));
  50.  
  51. hs.put(msg,compdb);
  52.  
  53. }
  54.  
  55. System.out.println("<Done>");
  56.  
  57. Collection<Computer_listing> values = hs.values();
  58. int i = 0;
  59.  
  60. for (Computer_listing temp : values) {
  61.     System.out.println("Item is enter in the db\n");
  62.     System.out.printf("Product Id: %s \n",temp.getId());
  63.     System.out.printf("Product Name: %s \n",temp.getName());
  64.     System.out.printf("Product Hardrive: %s \n",temp.getHardrive());
  65.     System.out.printf("Product Memory: %s \n",temp.getMemory());
  66.     System.out.printf("Product Speed: %s \n",temp.getSpeed());
  67.     System.out.println("--------------------");
  68.             i++;
  69.  
  70. }
  71.             } // close the if loop
nomad
In
Expand|Select|Wrap|Line Numbers
  1. compdb = new Computer_listing(msg);
are you creating a Computer_listing object using only one String as parameter?
Where is the constructor that takes that?
May 7 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: dougjrs | last post by:
I have a HashMap that is storing form data that will later be inserted into a database. I have been able to create the HashMap just fine, but I wanted to be able to take my HashMap and just "dump"...
1
by: Christian Gollwitzer | last post by:
Hi, I'm trying to loop over the elements in a hashmap of the STL-implementation by SGI. The point is, that because the key/value pair is stored as std::pair in the COntainer, the code becomes...
4
by: David | last post by:
Hi, I have created the following HashMap class. class HashMap: public hash_map<string, string, HashString, HashStringCompare> { public: HashMap(): hash_map<string, string, HashString,...
1
by: Sean | last post by:
Hi all, I have problem finding the .NET equivalent of HashMap in C++ STL. I CANNOT use Hashtable because I want (there will be more than one objects for the same key) objects with the same...
2
by: xor | last post by:
I'm doing up a school project using java, and am a little new to it (I've worked with other languages for years though). I've seen code posted by the instructor using HashMap like this... ...
6
by: bumrag | last post by:
This is the car dealership object relating to the coursework, there is also a separate object named car that i think i need to link to. The problem is on the addcar() method. Any help would be...
4
by: panos100m | last post by:
Hi these are the conents of my hashmap printing out the entrySet. entrySet1: OrderDate=10/30/2007, entrySet2: Level_0={Item_0={ItemTotal= 3.99, ItemName=test® in, ShipDate=10/31/2007,...
15
by: lbrtchx | last post by:
Hi, ~ I have found myself in need of some code resembling a Hashmap ~ This is easily done in Java this way: ~ import java.util.*; // __ public class JMith00Test{
1
by: evelina | last post by:
Hello, I need help. I have the following hashmap: HashMap<HashMap<Dimension, Integer>, String> mapList = new HashMap<HashMap<Dimension, Integer>, String>(); I want to extract Dimesion from the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.