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:1594) at java.base/java.util.Scanner.nextDouble(Scanner.java:2564) at CIRCLE.getData(Main.java:9) at CIRCLE.calculate(Main.java:22) at Main.main(Main.java:63)InputC5.00Expected output78.54Your Program Output
Question
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:1594) at java.base/java.util.Scanner.nextDouble(Scanner.java:2564) at CIRCLE.getData(Main.java:9) at CIRCLE.calculate(Main.java:22) at Main.main(Main.java:63)InputC5.00Expected output78.54Your Program Output
Solution
It seems like you're getting a NoSuchElementException. This exception is thrown by the Scanner class when you're trying to read an input but there is no input available.
In your case, it's happening at the getData method of the CIRCLE class, when you're trying to read a double.
Here's a step-by-step guide to fix this:
-
Check if you're calling
Scanner.nextDouble()without actually having a double to read. You might be calling this method more times than the number of doubles in your input. -
Make sure you're providing the correct input to your program. If your program is expecting a double, you should provide a double.
-
You can also add a check before calling
nextDouble()to see if there is a double available to read. You can do this using theScanner.hasNextDouble()method. This method returns true if the next token in the input can be interpreted as a double.
Here's how you can do it:
if(scanner.hasNextDouble()) {
double num = scanner.nextDouble();
// rest of your code
} else {
System.out.println("No double available to read");
}
This will prevent NoSuchElementException from being thrown.
Similar Questions
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:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at rohit.main(Main.java:15)
The Scanner's nextInt method may throw which exception? A. InputMismatchException B. StringIndexOutOfBoundsException C. UserNoListenException D. FileNotFoundException
Arrange the below code in correct order :import java.util.Scanner;public static void main(String args[]) {Scanner sc=new Scanner(System.in);public class Main{int num1 = sc.nextInt();System.out.println("The value is "+num1);}}
Which of the following methods is not present in the Scanner class in java?Options: Pick one correct answer from belownextLine();next();nextString();nextFloat();
import java.io.*;import java.util.Scanner; // Driver Classclass GFG { // main function public static void main(String[] args) { // Declare the variables int num; // Input the integer System.out.println("Enter the integer: "); // Create Scanner object Scanner s = new Scanner(System.in); // Read the next integer from the screen num = s.nextInt(); // Display the integer System.out.println("Entered integer is: " + num); }}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.