Question

In: Computer Science

My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...

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!");
            
       }
   }

Solutions

Expert Solution

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


Related Solutions

HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
I keep getting this error, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for...
I keep getting this error, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0    at simpleInheritance/simpInher.Dwelling$DriverTest.main(Dwelling.java:146) Can someone help me fix it? import java.io.BufferedReader;    import java.io.FileNotFoundException;    import java.io.FileReader;    import java.util.*;    import java.io.*;    import java.io.FileWriter;    import java.io.IOException;    class Dwelling {    /*    Declaring Variables    */    String streetAddress;    String city;    String state;    String zipCode;    int bedrooms;    double bathrooms;       /*   ...
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint....
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'test_ibfk_5' in the referenced table 'appointment', can you please tell me what is wrong with my code: -- Table III: Appointment = (site_name [fk7], date, time) -- fk7: site_name -> Site.site_name DROP TABLE IF EXISTS appointment; CREATE TABLE appointment (    appt_site VARCHAR(100) NOT NULL, appt_date DATE NOT NULL, appt_time TIME NOT NULL, PRIMARY KEY (appt_date, appt_time), FOREIGN KEY (appt_site)...
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
I keep get this exception error Exception in thread "main" java.lang.NullPointerException    at Quadrilateral.returnCoordsAsString(Quadrilateral.java:44)    at...
I keep get this exception error Exception in thread "main" java.lang.NullPointerException    at Quadrilateral.returnCoordsAsString(Quadrilateral.java:44)    at Quadrilateral.toString(Quadrilateral.java:51)    at tester1.main(tester1.java:39) In this program I needed to make a Point class to create a coordinate square from x and y. I also needed to make a Quadrilateral class that has an instance reference variable to Point .The Quadrilateral class then inherits itself to other classes or in this case other shapes like square, trapazoid. I thought I did it right but...
when i run the program on eclipse it gives me this error: Exception in thread "main"...
when i run the program on eclipse it gives me this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0    at SIM.main(SIM.java:12) how do I fix that ? (please fix it ) import java.util.*; import java.util.Scanner; import java.util.ArrayList; import java.io.File; import java.io.FileNotFoundException; import java.lang.Math; public class SIM{    public static void main(String[] args) throws FileNotFoundException {       int cacheSize = Integer.parseInt( args[1] ); int assoc = Integer.parseInt( args[2] ); int replacement = Integer.parseInt(...
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method...
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method is not static in class StaticInitializationBlock, please define the main method as:    public static void main(String[] args) This is what I'm working on class A { static int i; static { System.out.println(1); i = 100; } } public class StaticInitializationBlock { static { System.out.println(2); } public static void main(String[] args) { System.out.println(3); System.out.println(A.i); } }
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
Keep getting error where the code cannot read the text file and create an arraylist of...
Keep getting error where the code cannot read the text file and create an arraylist of objects from it. HouseListTester: import java.util.*; //Hard codes the criteria public class HouseListTester { static HouseList availableHouses; public static void main(String []args) { availableHouses = new HouseList("C:\\Users\\jvs34\\Downloads\\houses.txt"); Criteria c1 = new Criteria(1000, 500000, 100, 5000, 0, 10); Criteria c2 = new Criteria(1000, 100000, 500, 1200, 0, 3); Criteria c3 = new Criteria(100000, 200000, 1000, 2000, 2, 3); Criteria c4 = new Criteria(200000, 300000, 1500,...
I keep getting an error that I cannot figure out with the below VS2019 windows forms...
I keep getting an error that I cannot figure out with the below VS2019 windows forms .net framework windows forms error CS0029 C# Cannot implicitly convert type 'bool' to 'string' It appears to be this that is causing the issue string id; while (id = sr.ReadLine() != null) using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace Dropbox13 { public partial class SearchForm : Form { private List allStudent = new List(); public SearchForm() { InitializeComponent(); } private void SearchForm_Load(object...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT