Home > Back-end >  Infinity while loop on using scanner hasNext method
Infinity while loop on using scanner hasNext method

Time:11-04

I created a JDBC project for a Restaurant... The problem is it's going to infinity loop... I don't know what to do, I have been working on it for few days. The only solution I got is to scanner hasNext method but its also not working!!!

package com.restaurant.orders;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        System.out.println("WELCOME TO UR RESTAURANT");
        System.out.println("PLEASE, PLACE UR ORDER: ");
        ManageOrders.PlaceOrder();
        System.out.println("ANYTHING ELSE???");
        Scanner scanner=new Scanner(System.in);
        boolean c=true;
        while(true) {
            System.out.println("1. CancleOrder");
            System.out.println("2. CancleDish");
            System.out.println("3. ReplaceDish");
            System.out.println("4. DisplayOrderDetails");
            System.out.println("5. Nothing, Thank you!!");
            if(scanner.hasNextInt()) {
                int ch=scanner.nextInt();
                switch(ch) {
                    case 1:ManageOrders.CancleOrder();break;
                    case 2:ManageOrders.CancleDish();break;
                    case 3:ManageOrders.ReplaceDish();break;
                    case 4:ManageOrders.DisplayorderDetails();break;
                    case 5:c=false;break;
                }
            }
            if(c==false) {
                break;
            }
            else {
                continue;
            }
        }
        scanner.close();
    }
}

CodePudding user response:

The loop becomes infinite because somehow a non-integer value appears in the input and therefore hasNextInt() is always false.
It could make sense to change the type of the expected input to String and use hasNext() / next() to read a string value.
Also, the code may be improved:

  • switch statement is missing a default case;
  • if/else statement is redundant and may be removed;
  • while loop should not be infinite, it should check the status of c instead:
Scanner scanner=new Scanner(System.in);
boolean run = true;
while (run) {
    System.out.println("1. CancelOrder");
    System.out.println("2. CancelDish");
    System.out.println("3. ReplaceDish");
    System.out.println("4. DisplayOrderDetails");
    System.out.println("5. Nothing, Thank you!!");
    if (scanner.hasNext()) {
        String ch = scanner.next();
        switch(ch) {
            case "1": ManageOrders.CancleOrder(); break;
            case "2": ManageOrders.CancleDish();  break;
            case "3": ManageOrders.ReplaceDish(); break;
            case "4": ManageOrders.DisplayorderDetails(); break;
            case "5": run = false; break;
            default:  System.out.println("Use digits from 1 to 5 please"); break;
        }
    }
}
System.out.println("Bye");

CodePudding user response:

You need to get the user input on that way:

package com.restaurant.orders;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        System.out.println("WELCOME TO UR RESTAURANT");
        System.out.println("PLEASE, PLACE UR ORDER: ");
        ManageOrders.PlaceOrder();
        System.out.println("ANYTHING ELSE???");
        Scanner scanner = new Scanner(System.in);
        boolean c = true;
        while(true) {
            System.out.println("1. CancleOrder");
            System.out.println("2. CancleDish");
            System.out.println("3. ReplaceDish");
            System.out.println("4. DisplayOrderDetails");
            System.out.println("5. Nothing, Thank you!!");
            
            int ch = scanner.nextInt();
            switch(ch) {
                case 1:ManageOrders.CancleOrder();break;
                case 2:ManageOrders.CancleDish();break;
                case 3:ManageOrders.ReplaceDish();break;
                case 4:ManageOrders.DisplayorderDetails();break;
                case 5:c=false;break;
            }
            
            if(c==false) {
                break;
            }
            else {
                continue;
            }
        }
        scanner.close();
    }
}
  • Related