Exception in thread "main" java.util.NoSuchElementException: No line found Project Java Eclipse IDE

51 Views Asked by At

I'm working on a Java program using Eclipse IDE and encountering an issue with the "Exception in thread 'main' java.util.NoSuchElementException: No line found" error. This error occurs in my console output

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Controller.Client_Controller.menu(Client_Controller.java:94)
at Controller.Main_Controller.menu(Main_Controller.java:38)
at Controller.EShop2023.main(EShop2023.java:17)

The error is specifically linked to this line of code: sc.nextLine();
The one after the line: System.out.println("Invalid input. Please enter a valid number.");

Here is the relevant portion of the menu-handling code:

package Controller;

import java.util.ArrayList;
import java.util.Scanner;


import Model.Client;

public class Client_Controller {
    
    private ArrayList<Client> clientList = new ArrayList<>();
    private int i_Current;  // indice element en cours
    private int i_First;    // indice premier element affichable
    private int i_Last;     // indice dernier element affichable
    private int i_Aff;      // valeur elements affichables
    
    public static void main(String[] args) {
        Client_Controller controller = new Client_Controller();
        controller.menu();
    }
    
    public void menu() {
        int i_Choice = 0;
        Scanner sc = new Scanner(System.in);

        do {
            if (i_Aff != 0) {
                do {
                    C_CheckList();
                    System.out.println("\nClient's Menu");
                    System.out.println("***************");
                    System.out.println("1. Create");
                    System.out.println("2. Update");
                    System.out.println("3. Delete");
                    System.out.println("4. Next");
                    System.out.println("5. Previous");
                    System.out.println("6. Last");
                    System.out.println("7. First");
                    System.out.println("8. Select Client");
                    System.out.println("9. Exit");
                    System.out.print("Your choice ? : ");

                    if (sc.hasNextInt()) {
                        i_Choice = sc.nextInt();
                        sc.nextLine();

                        switch (i_Choice) {
                            case 1:
                                if (Client.getI_Pk() > 99) {
                                    System.out.println("Limit reached");
                                } else {
                                    C_Create(sc);
                                }
                                break;
                            case 2:
                                C_Update(sc);
                                break;
                            case 3:
                                clientList.get(i_Current).setB_Is_Deleted(true);
                                i_Aff--;
                                C_CheckList();
                                i_Current = i_Last;
                                break;
                            case 4:
                                C_Next_Client();
                                break;
                            case 5:
                                C_Previous_Client();
                                break;
                            case 6:
                                i_Current = i_Last;
                                break;
                            case 7:
                                i_Current = i_First;
                                break;
                            case 8:
                                C_Select();
                                break;
                            case 9:
                                System.out.println("Back to main menu...");
                                break;
                            default:
                                System.out.println("Wrong Choice, please select a new number");
                        }
                    } else {
                        System.out.println("Invalid input. Please enter a valid number.");
                        sc.nextLine();


                    }
                } while (i_Choice < 0 || i_Choice > 9);
            } else {
                do {
                    System.out.println("\nEmpty list");
                    System.out.println("Customer's Menu");
                    System.out.println("***************");
                    System.out.println("1. Create");
                    System.out.println("9. Exit");
                    System.out.print("Your choice ? : ");

                    if (sc.hasNextInt()) {
                        i_Choice = sc.nextInt();
                        sc.nextLine();

                        switch (i_Choice) {
                            case 1:
                                C_Create(sc);
                                break;
                            case 9:
                                System.out.println("Back to main menu...");
                                break;
                            default:
                                System.out.println("Wrong Choice, please select a new number");
                        }
                    } else {
                        System.out.println("Invalid input. Please enter a valid number.");
                        sc.nextLine();
                    }
                } while (i_Aff == 0);
            }
        } while (i_Choice != 9);
    }

I put System.out.println("Invalid input. Please enter a valid number."); because otherwise the menu in the console would go "crazy" and keep going up and down without being able to do anything. Putting this line is when the console goes crazy:

if (sc.hasNextInt()) {
i_Choice = sc.nextInt();
sc.nextLine();

Thank you in advance for any possible advice to help me to solve this problem.

0

There are 0 best solutions below