Java.util.NoSuchElementException No Exception for Such Elements

New to Java - just try to get a pen on it. The program is executed as follows:

What your age?23
23
What your name?Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at king.getName(king.java:25)
    at king.main(king.java:9)

The code that he is trying to run is below:

import java.util.*;

public class king {



    public static void main(String[] args){
        System.out.println(getAge());
        System.out.println(getName());
    }

    public static int getAge(){
        System.out.print("What your age?");
        Scanner scanner = new Scanner(System.in);
        String age = scanner.next();
        scanner.close();
        int numberAge = Integer.parseInt(age);
        return numberAge;

    }

    public static String getName(){
        System.out.print("What your name?");
        Scanner newScanner = new Scanner(System.in);
        String name = newScanner.next();
        newScanner.close();
        return name;
    }

}
+5
source share
4 answers

Do not use scanner.close()<- the source of your error!

delete lines scanner.close()andnewScanner.close()

From Java DOC:

When the scanner is closed, it will close its input source if the source implements the Closeable interface.

That means it closes System.in- a bad choice!

From the source code Scanner.javain the JDK, throwFor():

private void throwFor() {
    skipped = false;
    if ((sourceClosed) && (position == buf.limit()))
        throw new NoSuchElementException();
    else
        throw new InputMismatchException();
}

, , , , NoSuchElementException(). , IDEONE - position == buf.limit(), sourceClosed

+9

:

public class King {
  public static void main(final String[] args) {
    final Scanner scanner = new Scanner(System.in);
    System.out.println(getAge(scanner));
    System.out.println(getName(scanner));
  }

  public static int getAge(final Scanner scanner) {
    System.out.print("What your age?");
    final String age = scanner.next();
    final int numberAge = Integer.parseInt(age);
    return numberAge;
  }

  public static String getName(final Scanner scanner) {
    System.out.print("What your name?");
    final String name = scanner.next();
    return name;
  }
}

, Java .

+1

, alraedy @Aniket .

  • import java.util.*; import java.util.Scanner;, .
  • -, Scanner, . ( )

     import java.util.Scanner;
     public class SO6 {
         static Scanner scanner = new Scanner(System.in);
         public static void main(String[] args){
             System.out.println(getAge());
             System.out.println(getName());
             scanner.close();
         }
    
        public static int getAge(){
            System.out.print("What your age?");
            String age = scanner.next();
            int numberAge = Integer.parseInt(age);
            return numberAge;
    
        }
    
        public static String getName(){
            System.out.print("What your name?");
            String name = scanner.next();
            return name;
        }
    }
    
+1

First of all, to avoid a NoSuchElementException, you must perform a Scanner.hasNext () check before using Scanner.next (). Other comments, especially regarding the use of a single scanner, are also helpful.

+1
source

All Articles