Question
Why am I getting this error? ----jGRASP exec: java MathOP4 Enter your name: John James Hello John James How many numbers do you want to
Why am I getting this error?
----jGRASP exec: java MathOP4 Enter your name: John James Hello "John James" How many numbers do you want to enter? 4 Enter 4 numbers: 22,23,24,25 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextDouble(Scanner.java:2413) at MathOP4.main(MathOP4.java:15)
----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.
The error is shown below on line 15 (Bolded).
import java.util.Scanner;
public class MathOP4 {
public static void main(String[] args) { Scanner in = new Scanner(System.in); // scanner to read input System.out.print("Enter your name: "); String name = in.nextLine(); // read name from user System.out.println("Hello \"" + name + "\""); // say hello to user System.out.print("How many numbers do you want to enter? "); int n = in.nextInt(); // read how many numbers user wants to enter double average = 0; // initialize average to 0 System.out.print("Enter " + n + " numbers: "); for(int i = 0; i < n; ++i) { // iterate n times average += in.nextDouble(); // add user entered number to average } average /= n; // divide total with n to form average System.out.printf("Average of entered numbers is %.2f ", average); // display average to user }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started