占用浮点数组时获取InputMismatchException。

时间:2015-11-03 14:20:29

标签: java arrays inputmismatchexception

我有一个空的浮动数组。当我尝试输入带小数点的数据,例如17.5时,我收到此错误代码:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextFloat(Unknown Source)
at GradeCalc.GetInput(GradeCalc.java:27)
at GradeCalc.<init>(GradeCalc.java:43)
at GradeCalc.main(GradeCalc.java:47)

我的猜测是调试器说数据与数据类型不匹配。这很奇怪,因为17.5是一个浮点数。这里发生了什么? 错误位于:

 >GradeCalc()
 >>GetInput()
 >>>gradeValue[i] = sc.nextFloat();
 public class GradeCalc {

float gradeValue[] = new float[19];
String gradeName[] = { "Sv1", "Sv2", "En5", "En6", "En7", "Ma1c", "Ma2c",
        "Ma3c", "Ma4", "Ke1", "Ke2", "Bi1", "Bi2", "Fy1", "Fy2", "Fr4",
        "Prog", "Körsång", "I&H" };

String Y;

Scanner sc = new Scanner(System.in);

public void Calc(float gradeValue[]) {

    float average_grade, sumofgrade;

    for (int i = 0; i < gradeValue.length; i++) {
        sumofgrade = gradeValue[i];
    }

}

public void GetInput() {
    for (int i = 0; i <= 19; i++) {
        System.out.println("Enter grade value for " + gradeName[i]);
        gradeValue[i] = sc.nextFloat();
    }
    Calc(gradeValue);
}

public void AtStart() {
    System.out.println("Press Enter Y to Start: ");
    Y = sc.next();

    if (Y == "Y") {
        GetInput();
    }else{ System.exit(0);}
}

public GradeCalc() {
    //AtStart();
    GetInput();
}

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

}

0 个答案:

没有答案
相关问题