1

In my java code when i am trying to get input as String So Scanner.next() and when i run it Exception in thread "main" java.util.NoSuchElementException this is my code

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.*;

public class App {
    static int choice;
    static Thread t;
    static boolean isCompleteLoading;

   public static void main(String[] args) throws Exception {
      System.out.println();
        mainMenu ();
    }

    static void Register () throws FileNotFoundException, IOException {

        Scanner input = new Scanner(System.in);
        Student student1 = new Student();

        System.out.println("\n\t\t\t\t\t\t       WELCOME TO MAIN MENU");
        System.out.println("\t\t\t\t\t\t     ------------------------");

        System.out.print("\t\tEnter Your Name: ");
        student1.studentName = input.next();
        System.out.print("\t\tEnter Your Phone Number: ");
        student1.phoneNumber = input.nextDouble();
        System.out.print("\t\tEnter Your address: ");
        student1.address = input.next();
        System.out.print("\t\tEnter Your school name: ");
        student1.schoolName = input.next();
        System.out.print("\t\tHow do you wish to pay");
        System.out.print("\n\t\t\t 1.Full");
        System.out.print("\n\t\t\t 2.Half");
        System.out.print("\n\n\t\tEnter Your choice: ");
        student1.paymentType = input.nextInt();
        processing();

        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Person.txt"));
        out.writeObject(student1);
        input.close();
    }

    public static void processing() {
        t=new Thread() {
            public void run() {
                System.out.print("Processing---------");
                for(int i = 0;i <= 100;i++){
                    if(i < 10){
                        System.out.print(i+"%");
                        System.out.print("\b\b");
                    }
                    else if(i >= 10 && i <= 99){
                        System.out.print(i+"%");
                        System.out.print("\b\b\b");
                    }
                    else{
                     System.out.println(i+"%");
                     isCompleteLoading = true;
                    }
                    try {
                        t.sleep(100);
                    } catch(Exception e) {

                    }
                }
            }
        };
        t.start();
  }

    public static void mainMenu () throws FileNotFoundException, IOException {

        Scanner in = new Scanner(System.in);

        System.out.println("\t\t\t\t\t\t ANATHA STUDENT MANAGEMENT SYSTEM");
        System.out.println("\n\t\t\t\t\t\t       WELCOME TO MAIN MENU");
        System.out.println("\n\n\t\t\t\t 1.Register New Student");
        System.out.println("\t\t\t\t 2.Find Student");
        System.out.print("\n\t\t\t Enter Your choice: ");

        choice = in.nextInt();

        in.close();

        switch (choice) {
            case 1:
                Register ();
                break;

            default:
                break;
        }
    }
}

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 App.Register(App.java:27)
    at App.mainMenu(App.java:90)
    at App.main(App.java:15)

why is this error occurred ?

  • 2
    Does this answer your question? [Exception in thread "main" java.util.NoSuchElementException](https://stackoverflow.com/questions/15398703/exception-in-thread-main-java-util-nosuchelementexception) – iambk Jan 23 '22 at 08:54
  • no i use a single Scanner object for all of inputs but it didnt solved –  Jan 23 '22 at 10:32
  • 1
    I tried your code with single Scanner instance and it is working. Could you update the question with what you've tried? – iambk Jan 23 '22 at 10:52

1 Answers1

0

In your Register() function, you try to create a Scanner on System.in. However, before calling Register() in mainMenu(), you close the Scanner created at the beginning of mainMenu(). Due to how InputStreams in Java work, this also closes System.in, therefore trying to create a new Scanner on it doesn't work. This is why it's best to only have one Scanner for command line programs - if you need it in multiple functions, you can either make it a static variable or pass it down as an argument for those functions.

Itisdud
  • 41
  • 7