You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用nextFloat()时抛出java.util.InputMismatchException异常的解决咨询

Fixing InputMismatchException with nextFloat() (Without nextLine() + parseFloat())

Hey there! Let's break down why you're hitting this InputMismatchException at in.nextFloat() and fix it without using the nextLine() + parseFloat() approach you mentioned is off-limits.

Common Root Cause: Locale Mismatch

The most likely issue here is a locale setting conflict. For example, if your system's default locale uses a comma (,) as the decimal separator (common in European countries like Germany, France, etc.), but you're entering grades with a dot (.), Scanner.nextFloat() won't recognize the input as a valid float and throws the exception.

Solution 1: Force Scanner to Use English Locale

You can explicitly set the Scanner to use an English locale, which uses dots as decimal separators, regardless of your system's default settings. Here's how to modify your code:

First, add the import for Locale:

import java.util.Locale;

Then update your main method to set the locale right after creating the Scanner:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    in.useLocale(Locale.ENGLISH); // Add this line to enforce dot as decimal separator
    System.out.println("Number of Grades? ");
    float[] vals= new float[in.nextInt()];
    for (int i = 0; i < vals.length ; i++) {
        System.out.println("Grade " + (i+1) + "? ");
        vals[i]=in.nextFloat(); // This will now correctly parse inputs like 7.3
    }
    // Rest of your code...
}

Solution 2: Add Input Validation

To make your program more robust (and handle cases where users enter non-numeric values), you can use Scanner.hasNextFloat() to check if the input is a valid float before trying to parse it. This works alongside the locale fix to cover more edge cases:

for (int i = 0; i < vals.length ; i++) {
    System.out.println("Grade " + (i+1) + "? ");
    // Keep asking until valid input is provided
    while (!in.hasNextFloat()) {
        System.out.println("Oops, that's not a valid number. Please try again:");
        in.next(); // Skip the invalid input to avoid an infinite loop
    }
    vals[i] = in.nextFloat();
}

Bonus: Fix a Hidden Bug in removeMins

I noticed a small bug in your removeMins method that would break your average calculation later. The line:

float[] res = new float[vals.length - n];

runs after your while(n > 0) loop, where n has already been decremented to 0. This means your result array will be the same length as the original array, and unused positions will be filled with 0.0f—which the average method will incorrectly include in its calculation.

Fix this by saving the original value of n before modifying it:

static float[] removeMins(int n, float[] vals) {
    int originalRemoveCount = n; // Save the original number of mins to remove
    while (n > 0) {
        int a = indexOfMin(vals);
        vals[a] = -1;
        --n;
    }
    float[] res = new float[vals.length - originalRemoveCount]; // Use the original value
    int b = 0;
    for (int i = 0; i < vals.length; i++)
        if (vals[i] != -1) {
            res[b] = vals[i];
            ++b;
        }
    return res;
}

Combining these fixes should resolve your InputMismatchException and ensure your grade average calculation works correctly.

内容的提问来源于stack exchange,提问作者Rui

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:41:03