java - input system where the user inputs the array position of the object followed by a # to indiacte quantity but it gives me an error -


its tuck shop program!

public void sale() {         if (!ingredients.isempty()) {             printfood();             string choice = joptionpane.showinputdialog("enter choices seperatad # indicate quantity");             string[] choices = choice.split(" ");             string[] ammounts = choice.split("#");             (int = 0; < choices.length; i++) {                 int foodpos = (integer.parseint(choices[i])) - 1;                 int ammount = integer.parseint(ammounts[i+1]);                 try {                     foods.get(foodpos).sale(ammount);                 } catch (indexoutofboundsexception e) {                     system.out.println("ingredient not exsist");                 }             }         }       } 

http://paste.ubuntu.com/5967772/

gives error

exception in thread "main" java.lang.numberformatexception: input string: "1#3" @ java.lang.numberformatexception.forinputstring(numberformatexception.java:65) @ java.lang.integer.parseint(integer.java:492) @ java.lang.integer.parseint(integer.java:527)

you're splitting same string twice, strings immutable you're getting 2 different arrays while original string stays same. therefore, if have input like:

1#3 2#4 

you splitting (" ") yield:

1#3 2#4 

which try parse integer later @ line:

int foodpos = (integer.parseint(choices[i])) - 1; 

that throwing numberformatexception. need re-split each individual array element ("#"), rather source string.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -