Hashmap and finding data with a prompt

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

class Computer_listing{

private String id;

private String name;

privateint harddrive;

privateint memory;

privatefloat 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;

}

publicint getHardrive(){

return this.harddrive;

}

publicint getMemory(){

return this.memory;

}

publicfloat getSpeed(){

return this.speed;

}

publicvoid setId(String cd){

this.id = cd;

}

publicvoid setName(String cn){

this.name = cn;

}

publicvoid setHarddrive(Integer hd){

this.harddrive = hd;

}

publicvoid setMemory(Integer me){

this.memory = me;

}

publicvoid setSpeed(float sp){

this.speed = sp;

}

}

publicclass ComputerInvent{

publicstaticvoid 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

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

sandyR

[9590 byte] By [SandyReda] at [2007-11-27 3:37:31]
# 1
I think you entered datas when you run this program firstly,then you tried second run and expected to get first run's datas if so it's impossible!,because yo did not save datas in any files or database..
coach80a at 2007-7-12 8:40:47 > top of Java-index,Java Essentials,New To Java...
# 2
if that is true why won't the data that is in the main show up on a search.sandyR
SandyReda at 2007-7-12 8:40:47 > top of Java-index,Java Essentials,New To Java...
# 3

Hi,

I don't understand exactly what you mean in your reply.,

but may be this helps you,

what is going on your program is ;

you entered some datas,and these are stored in memory then program exits and hashmap variable in memory deallocated,if you put some choices after entering data(like display,as you do this with the "stop" argument) you can get the expected result.

hope I am clear..

Message was edited by:

coach80

coach80a at 2007-7-12 8:40:47 > top of Java-index,Java Essentials,New To Java...