In: Computer Science
My code works in eclipse, but not in Zybooks. I keep getting this error.
Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34)
Your output
Welcome to the food festival! Would you like to place an order?
Expected output
This test case should produce no output
in java
import java.util.Scanner;
public class Main
{
public static void display(String menu[])
{
for(int i=0; i<menu.length;
i++)
{
System.out.println (i + " - " + menu[i]);
}
System.out.print ("Enter the
number for your selection: ");
}
public static void main (String[] args)
{
String mainMenu[] =
{"Nothing", "Appetizer", "Main Course", "Dessert"};
String dessertMenu[] =
{"Nothing", "Chocolate Cake", "Ice Cream", "Brownie"};
String appetizerMenu[] =
{"Nothing", "Shrimp", "Bread", "Mozerella Sticks"};
String mainCourseMenu[] =
{"Nothing", "Burgers", "Fish", "Pasta"};
String toppingsMenu[] =
{"Nothing", "Cheese", "Sprinkles", "Syrup"};
String list = "";
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.println ("Welcome
to the food festival!");
System.out.print ("Would you
like to place an order?");
String ans = sc.next();
if(ans.equals("NO"))
{
System.out.println ("NO:(user answered NO)");
System.out.println ("Thank you for stopping by, maybe next time
you'll sample our menu");
return;
}
if(ans.equals("YES"))
{
System.out.println ("YES:(user answered YES)");
System.out.print ("What is your name for the order?");
String
name = sc.next();
int m,
n, k;
System.out.println ("Select from menu, " + name);
while(true)
{
display(mainMenu);
n = sc.nextInt();
if(n==0) break;
switch(n)
{
case 1:
System.out.println ("Appetizer Menu:");
display(appetizerMenu);
m = sc.nextInt();
list = list + "Appetizer:[" + appetizerMenu[m] + ": ";
while(true){
display(toppingsMenu);
k = sc.nextInt();
if(k==0) break;
list = list + toppingsMenu[k] + " ";
}
list = list + "]\n";
break;
case 2:
System.out.println ("Main Course Menu:");
display(mainCourseMenu);
m = sc.nextInt();
list = list + "Main Course: [" + mainCourseMenu[m] + "]\n";
break;
case 3:
System.out.println ("Dessert Menu:");
display(dessertMenu);
m = sc.nextInt();
list = list + "Dessert: [" + dessertMenu[m] + "]\n";
}
}
}
System.out.println ("Here is your order ");
System.out.println (list);
System.out.println ("Enjoy your meal!");
}
}
step1 Add sc.nextLine() in line 33 ie in the first whileloop as the error is in line 34 . It will solve the problem .
step2 Use try Catch method block in the display method of main to avoid abrupt execution of program and this will handle Excepion in thread "main" also it helps you to configure the exact mistakes while coding by defining a block of code to be executed
No such Element Exception occurs while using Enumeration or iteration when the element requested doesnot exist which can be solved by using next() method as it returns the next element in iteration
any way the output will have
Welcome to the Food Festival! wuld you like to place an order?
later according to input given either "yes" or "no" the program proceeds to give output ie if ans=yes
user answered yes
what is your name for the order and so on the program carryon by taking menu
if ans=no
user answered No
Thankyou for stopping by, maybe next time you'll sample our menu
hope it helped you