Search code examples
javacompiler-errorsmismatch

How do I fix my "java.util.InputMismatchException" error?


All I need it to do is loop again so the user can continuously use the program if they to. Let me know if there are any reference that I can read up to, to help me understand more about this problem. Thanks in advance.

import java.util.Scanner;

public class Module3Assignment1 {

    // public variables
    public static String letterChosen;
    public static int loop = 0;
    public static double radius, area;
    public static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        // tells user what the program is about
        System.out.println("Welcome to the Round Object Calculator");
        System.out.println("This program will calculate the area of a circle of the colume of a sphere.");
        System.out.println("The calculations will be based on the user input radius.");
        System.out.println("");

            // loops while the user wants to calculate information
            while (loop == 0){

                Input();
                System.out.print(Answer());

                System.out.println("Do you want to calculate another round object (Y/N)? ");
                String input = scanner.next().toUpperCase();
                    if (input == "N"){
                        loop = 1;
                        }
            }

        // ending message/goodbye
        Goodbye();
        scanner.close();

    }

    private static void Input(){

        // prompts user for input
        System.out.print("Enter C for circle or S for sphere: ");
        letterChosen = scanner.nextLine().toUpperCase();
        System.out.print("Thank you. What is the radius of the circle (in inches): ");
        radius = scanner.nextDouble();

    }

    private static double AreaCircle(){

        // calculates the area of a circle
        area = Math.PI * Math.pow(radius, 2);
        return area;

    }

    private static double AreaSphere(){

        // calculates the area of a sphere
        area = (4/3) * (Math.PI * Math.pow(radius, 3));
        return area;

    }

    private static String Answer(){

        //local variables
        String answer;

        if(letterChosen == "C"){
            // builds a string with the circle answer and sends it back
            answer = String.format("%s %f %s %.3f %s %n", "The volume of a circle with a radius of", radius, "inches is:", AreaCircle(), "inches");
            return answer;
        }else{
            // builds a string with the sphere answer and sends it back
            answer = String.format("%s %f %s %.3f %s %n", "The volume of a sphere with a radius of", radius, "inches is:", AreaSphere(), "cubic inches");
            return answer;
        }
    }

    private static String Goodbye(){

        // local variables
        String goodbye;

        // says and returns the goodbye message
        goodbye = String.format("%s", "Thank you for using the Round Object Calculator. Goodbye");
        return goodbye;
    }

}

The below is the console output and the error I am getting after execution

Welcome to the Round Object Calculator
This program will calculate the area of a circle of the colume of a sphere.
The calculations will be based on the user input radius.

Enter C for circle or S for sphere: C
Thank you. What is the radius of the circle (in inches): 12
The volume of a sphere with a radius of 12.000000 inches is: 5428.672 cubic inches 
Do you want to calculate another round object (Y/N)? 
Y
Enter C for circle or S for sphere: Thank you. What is the radius of the circle (in inches): C
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextDouble(Scanner.java:2387)
    at Module3Assignment1.Input(Module3Assignment1.java:48)
    at Module3Assignment1.main(Module3Assignment1.java:24)

Solution

  • import java.util.Scanner;
    
    public class Module3Assignment1 {
    // public static variables are discouraged... 
    private static char letterChosen; //char takes less memory 
    private static char useAgain = 'Y'; //just use the answer to loop... 
    private static double radius, area;
    private static String answer;
    private  static Scanner scanner = new Scanner(System.in);
    
    
    //you might want to clear the screen after the user gave an answer to another round object
    private static void clearScreen(){
        for(int i =0;i<50;i++){System.out.print("\n");}
    }
    
    public void input(){
    
        // prompts user for input
        System.out.print("Enter C for circle or S for sphere: ");
        letterChosen = scanner.next().charAt(0);
        System.out.print("Thank you. What is the radius of the circle (in inches): ");
        radius = scanner.nextDouble();
        this.answer= answer(letterChosen);
    
    }
    
    public double areaCircle(double radius){
    
        // calculates the area of a circle
        area = Math.PI * Math.pow(radius, 2);
        return area;
    
    }
    
    public double areaSphere(double radius){
    
        // calculates the area of a sphere
        area = (4/3) * (Math.PI * Math.pow(radius, 3));
        return area;
    
    }
    
    public String answer(char letterChosen){
    
        //local variables
        String answer = "";
        if(letterChosen=='c'||letterChosen=='C'){
            answer = String.format("%s %f %s %.3f %s %n", "The volume of a circle with a radius of", radius, "inches is:", areaCircle(radius), "inches");
        }else{
            answer = String.format("%s %f %s %.3f %s %n", "The volume of a sphere with a radius of", radius, "inches is:", areaSphere(radius), "cubic inches");
        }
        return answer;
    }
    
    private static String goodbye(){
    
        // local variables
        String goodbye;
    
        // says and returns the goodbye message
        goodbye = String.format("%s", "Thank you for using the Round Object Calculator. Goodbye");
        return goodbye;
    }
    
    public static void main(String[] args) {
    
        // tells user what the program is about
        System.out.println("Welcome to the Round Object Calculator");
        System.out.println("This program will calculate the area of a circle of the colume of a sphere.");
        System.out.println("The calculations will be based on the user input radius.");
        System.out.println("");
        Module3Assignment1 ass1 = new Module3Assignment1();
    
            // loops while the user wants to calculate a round object
            while (useAgain == 'Y'||useAgain=='y'){
    
                ass1.input();
                System.out.print(answer);
                System.out.println("Do you want to calculate another round object (Y/N)? ");
                useAgain = scanner.next().charAt(0);
                System.out.println(useAgain);
                clearScreen();
            }
    
        // ending message/goodbye
        System.out.println(goodbye());
        scanner.close();
    
    }
    

    }

    Some things that I changed:

    • I used char instead of String. String takes up more memory than char.
    • added clearScreen() method which "clears" the screen when you're using console.
    • I added a parameter radius to areaSphere and areaCircle methods. This makes the methods reusable.

    • I changed all the public static variables to private static. using public static variables is HIGHLY DISCOURAGED. You may read this to find out why.

    • and to prevent public static variables, I created an instance of Module3Assignment1 instead of having everything in static.

    • changed the casing of method names. Please follow camel-casing, which means that the first letter of the method is lowercase and the other words will have the first letter in uppercase (e.g. input(), areaSphere() )

    A comment about comparing Strings:

    == compares REFERENCES TO THE OBJECT , NOT VALUES

    use .equals() or .equalsIgnoreCase() if you want to compare the values of two Strings. here is a sample syntax:

    if(string1.equals(string2)){
    //do something
    }