-
int and String, help me
Hi, I'm making this class to take a customer's order for a project, but I hit a snag. Basically I'm asking a customer for the amount of pizzas they want, then the size, the first topping, and the last topping.
The amount of pizzas they want will initiate the amount of times my "for loop" will run to ask the same round of questions.
for the size of the pizzas, I've assigned (1)small (2)medium (3)large. The customer enters the respective number, and i take the input as
Code:
int size=JOptionpane.showInputDialog("Whats the size?(1)small(2)medium(3)large" )
I'll get a int number between 1-3, but now how do i assign a string value to an integer?
like how do i get the input of "1" to mean "small"?
Thanks
-
Step 1 is quite straight forward. You can use the parseInt function on the Integer class:
Code:
int number = Integer.parseInt(someString);
Remember to check for NumberFormatExceptions if the user types in rubbish.
The next bit of making the 1 mean small can be as simple as defining a constant in your class but you may want to investigate using an enum instead.
Hope this helps.