Ravin
Ravin

Reputation: 626

java.util.NoSuchElementException No Such Element Exception

New to Java - just trying to get a handle on it. The program is executing as follows:

What's your age?23
23
What's 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 it 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's 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's your name?");
        Scanner newScanner = new Scanner(System.in);
        String name = newScanner.next();
        newScanner.close();
        return name;
    }

}

Upvotes: 5

Views: 12854

Answers (4)

Gunnarr
Gunnarr

Reputation: 101

First of all, to avoid NoSuchElementException you should perform Scanner.hasNext() check before using Scanner.next(). Other comments especially about using single scanner are also useful.

Upvotes: 1

Aniket Inge
Aniket Inge

Reputation: 25705

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

remove the lines scanner.close() and newScanner.close()

From Java DOCs:

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

Which means it closes the System.in - bad choice!

From source code of Scanner.java in JDK, throwFor() is:

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

Clearly, if we have reached the end of input, OR if the source is closed, then we get the NoSuchElementException(). I am pretty sure the one at IDEONE has happened because of position == buf.limit() rather than sourceClosed

Upvotes: 9

Bhushan
Bhushan

Reputation: 6181

I think alraedy @Aniket has given a very good answer.

  • First of all instead of import java.util.*; you can use import java.util.Scanner; so it will not import unnecessary classes.
  • Second thing is that you can have a single Scanner like i have shown in the example.(so you can close it)

     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's your age?");
            String age = scanner.next();
            int numberAge = Integer.parseInt(age);
            return numberAge;
    
        }
    
        public static String getName(){
            System.out.print("What's your name?");
            String name = scanner.next();
            return name;
        }
    }
    

Upvotes: 1

Christophe Roussy
Christophe Roussy

Reputation: 16999

Try this:

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's 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's your name?");
    final String name = scanner.next();
    return name;
  }
}

Also note that java classes should be capitalized.

Upvotes: 1

Related Questions