java - how to get a scanner to read a pre-set variable -


i trying use scanner user input add new item stock list have created, item must possess attributes itemid, itemdesc, price, quantity, , reorderlevel.

how go reading user input, , recognising 1 of variables i've created, , adding list?

i've had go, doesnt appear recognise them variables

any appreciated. thanks

my attempt:

else if (i==1)     {         stocklistinterface.doadditem(item);          system.out.println("add new item");         system.out.println("****************");          system.out.println("enter id    :>");         scanner scanner1 = new scanner(system.in);         string itemid = scanner1.nextline();          system.out.println("enter description    :>");         scanner scanner2 = new scanner(system.in);         string itemdesc = scanner2.nextline();          system.out.println("enter price    :>");         scanner scanner3 = new scanner(system.in);         string price = scanner3.nextline();          system.out.println("enter quantity    :>");         scanner scanner4 = new scanner(system.in);         string quantity = scanner4.nextline();          system.out.println("enter re-order level    :>");         scanner scanner5 = new scanner(system.in);         string reorderlevel = scanner5.nextline();          system.out.println("enter another? (y/n)    :>");     } 

you don't need create new scanner every time take input. can use defined scanner follows:

else if (i==1) {     stocklistinterface.doadditem(item);      system.out.println("add new item");     system.out.println("****************");      system.out.println("enter id    :>");     scanner scanner1 = new scanner(system.in);     string itemid = scanner1.nextline();      system.out.println("enter description    :>");     string itemdesc = scanner1.nextline();      system.out.println("enter price    :>");     string price = scanner1.nextline();      system.out.println("enter quantity    :>");     string quantity = scanner1.nextline();      system.out.println("enter re-order level    :>");     string reorderlevel = scanner1.nextline();      system.out.println("enter another? (y/n)    :>"); } 

also should consider adding 1 more line @ end, input of 'y' or 'n' follows:

string addanother = scanner1.nextline(); 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -