eclipse – Java Error – Exception in thread main java.util.NoSuchElementException

eclipse – Java Error – Exception in thread main java.util.NoSuchElementException

Lets dissect the error message the runtime helpfully provided:

Exception in thread main java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at project1.inches(project1.java:82)

This means that your code, on line 82 of project1.java, invoked the method java.util.Scanner.nextInt(), which signaled an error condition by throwing a java.util.NoSuchElementException.

To figure out why the method throws this exception, lets read its documentation:

public int nextInt()

Scans the next token of the input as an int.

An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix), where radix is the default radix of this scanner.

Returns:

the int scanned from the input

Throws:

InputMismatchException – if the next token does not match the Integer regular expression, or is out of range

NoSuchElementException – if input is exhausted

IllegalStateException – if this scanner is closed

Therefore, a NoSuchElementException indicates that you tried to read another number, but System.in was exhausted.

Thats weird, because System.in is usually connected to the keyboard, which provides an inexhaustible supply of keypresses …

The most likely cause is therefore that you launched the program without a console, which is a setting in your launch configuration:

Allocate

I dont seem to see any error when I run the code. Here is the code, I just tested it to see if it runs correctly and it does, I made only a few changes.

public class Main {

    public static void main(String[] args) {
        inches();
    }

    public static void inches() {
        //Define object variables
        int vinch = 0;
        float vcent;

        System.out.println(n*******************************************************n);
        System.out.println(Inch to Centimeter Conversionnn------------------------n);

        //Read length (inches)
        System.out.println(Enter a length (in inches):);
        Scanner scanInches = new Scanner(System.in);

        if (scanInches.hasNextInt())
        {
            vinch = scanInches.nextInt();
        }

        //Convert input from inches to centimeters & print as float value
        vcent = (float) (vinch * 2.54);
        System.out.println(vinch +  inches is equal to  + vcent +  centimeters.);

    }
}

As you can see I removed the scanner close method from the bottom of the inches method as you dont want to close it. I also added a check to see if there was nextInt to grab from the input of the user just to be safe. Also according to google, the conversion rate is 2.54, not 2.84 so I changed that too.

eclipse – Java Error – Exception in thread main java.util.NoSuchElementException

You do not want to close System.in

  // scanInches.close();  comment this out

Also you should test for the presence of an int using

if (scanInches.hasNextInt())
    vinch = scanInches.nextInt();

Leave a Reply

Your email address will not be published.