Java在stringinput上抛出异常

时间:2016-09-20 00:58:34

标签: java exception

所以我正在为我班级创建一个gpa计算器。 一切似乎看起来很好,似乎它会工作正常,但由于一些奇怪的原因,当我尝试运行它我得到这些错误。 什么导致他们? 这让我非常沮丧。

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 GpaCalculator.main(GpaCalculator.java:22)

源代码:

import java.util.*;

public class GpaCalculator {
    public static void main(String[] args) {

        Scanner numberinput = new Scanner(System.in);
        Scanner stringinput = new Scanner(System.in);
        // this program will calculate the gpa of four classes
        double A,B,C,D,F;
        double grade,grade2,grade3,grade4;
        double gpa = 0,name,Course2,Course3,Course4;
        int Course1;

        A = 4.0;
        B = 3.0;
        C = 2.0;
        D = 1.0;
        F = 0.0;

        System.out.println("Please enter your name>>>");
        name = stringinput.nextDouble();

        System.out.println("Please enter your course>>>");
        Course1 = stringinput.nextInt();

        System.out.println("What is the weight of this class? Normal, Honors, or AP?>>>");
        String weight = stringinput.nextLine();

        System.out.println("What is your grade in the class?>>>");
        grade = numberinput.nextDouble();

        if (weight.equalsIgnoreCase("honors"))
            gpa = gpa + 1;
        else if (weight.equalsIgnoreCase("Ap"))
            gpa = gpa + 1.5;
        else if (weight.equalsIgnoreCase("normal"));

        if (grade >= 100)
            grade = A; 
        else if (grade >= 91)
            grade = B; 
        else if (grade >= 83)
            grade = C; 
        else if (grade >= 75 )
            grade = D; 
        else if (grade >= 67)
            grade = F; 

        System.out.println("Please enter your course>>>");
        Course2 = stringinput.nextDouble();

        System.out.println("What is the weight of this class? Normal, Honors, or AP?>>>");
        String weight2 = stringinput.nextLine();

        System.out.println("What is your grade in the class?>>>");
        grade2 = numberinput.nextDouble();

        if (weight2.equalsIgnoreCase("honors"))
            gpa = gpa + 1;
        else if (weight2.equalsIgnoreCase("Ap"))
            gpa = gpa + 1.5;
        else if (weight2.equalsIgnoreCase("normal"));

        if (grade2 >= 100)
            System.out.println(A); 
        else if (grade2 >= 91)
            System.out.println(B);
        else if (grade2 >= 83)
            System.out.println(C);
        else if (grade2 >= 75 )
            System.out.println(D);
        else if (grade2 >= 67)
            System.out.println(F);

        System.out.println("Please enter your course>>>");
        Course3 = stringinput.nextDouble();

        System.out.println("What is the weight of this class? Normal, Honors, or AP?>>>");
        String weight3 = stringinput.nextLine();

        System.out.println("What is your grade in the class?>>>");
        grade3 = numberinput.nextDouble();

        if (weight3.equalsIgnoreCase("honors"))
            gpa = gpa + 1;
        else if (weight3.equalsIgnoreCase("Ap"))
            gpa = gpa + 1.5;
        else if (weight3.equalsIgnoreCase("normal"));

        if (grade3 >= 100)
            System.out.println(A); 
        else if (grade3 >= 91)
            System.out.println(B);
        else if (grade3 >= 83)
            System.out.println(C);
        else if (grade3 >= 75 )
            System.out.println(D);
        else if (grade3 >= 67)
            System.out.println(F);

        System.out.println("Please enter your course>>>");
        Course4 = stringinput.nextDouble();

        System.out.println("What is the weight of this class? Normal, Honors, or AP?>>>");
        String weight4 = stringinput.nextLine();

        System.out.println("What is your grade in the class?>>>");
        grade4 = numberinput.nextDouble();

        if (weight4.equalsIgnoreCase("honors"))
            gpa = gpa + 1;
        else if (weight4.equalsIgnoreCase("Ap"))
            gpa = gpa + 1.5;
        else if (weight4.equalsIgnoreCase("normal"));

        if (grade4 >= 100)
            System.out.println(A); 
        else if (grade4 >= 91)
            System.out.println(B);
        else if (grade4 >= 83)
            System.out.println(C);
        else if (grade4 >= 75 )
            System.out.println(D);
        else if (grade4 >= 67)
            System.out.println(F);

        gpa = grade + grade2 + grade3 + grade4 / 4;

        System.out.println(name);
        System.out.println(Course1 + ": " + grade);
        System.out.println(Course2 + ":" + grade2);
        System.out.println(Course3 + ": " + grade3);
        System.out.println(Course4 + ":"  + grade4);
        System.out.println(gpa);
    }
}

1 个答案:

答案 0 :(得分:0)

你真的只需要一个Scanner个对象。每种类型不需要两个Scanner个对象。

// This is your input in general.
Scanner input = new Scanner(System.in);

// This is a string input.
System.out.println("Enter a string:");
String str = input.nextLine();

// This is a double input.
System.out.println("Enter a double:");
Double dbl = input.nextDouble();