0

So I'm trying to use the scanner util in java to get some user inputs and then run a line of code with those values. I still don't know how to save an int so I can use that value later on. here is an example of what I'm trying to accomplish:

import java.util.Scanner;

class myRestaurant {

public static void main(String[] args){

String[] food;
String[] drink;
Scanner foodOrder = new Scanner(System.in);
Scanner drinkOrder = new Scanner(System.in);

food = new String[4];
drink = new String[4];

food[0]= "Hamburger";
food[1]= "Pizza";
food[2]= "Hot Dogs";
food[3]= "Salad";

drink[0]="Soda";
drink[1]="Water";
drink[2]="Tea";
drink[3]="Lemonade";

System.out.println("Please select your food followed by your drink");

System.out.println("Your " + food[foodOrder.nextInt()] + " with " + drink[foodOrder.nextInt()] + " will be ready shortly");
}}

Output

Output: "Please select your food followed by your drink"

User Inputs 2 separate int's (2) (3)

Output: "Your Hot Dogs with Lemonade will be ready shortly"

I dont know how to make it so:

Output: Please select your food Input: (value 1) Output: Now select your drink Input: (value 2)

Output: "Your (value 1) with (value 2) will be ready shortly"

Any hints on how i can get this done will be gladly appreciated.

John Joe
  • 10,340
  • 9
  • 48
  • 104
  • Don't create multiple `Scanner` objects. Just create one and prompt for more inputs by using `nextInt` or similar to get the input. – Andrew Li May 12 '17 at 02:52
  • Also, it's cleaner to set the local array values at instantiation level if you know what those values are going to be, e.g. `String[] food = {"Hamburger","Pizza","Hot Dogs","Salad"};`. You are going to need 2 arrays, 2 ints, 1 scanner, a set of instructions followed by assigning the scanner int (e.g. `foodOrder = scanner.nextInt();`), and finally don't get caught with off by one errors. Remember the first index of the array begins at 0. – Fiddle Freak May 12 '17 at 03:08

0 Answers0