0

so I'm coding a small program to do the average of the best grades of a student, when I run it, I get this error Exception in thread "main" java.util.InputMismatchException.

I saw a post where it was said to use nextLine and then use parseInt ( in this case parseFloat) , but I'm not allowed to use that in here, so I'm looking for another opinion of what I could do.

Code:

import java.util.Scanner;

public class G2a {

static float average(float[] vals) { //Returns the average of an array;
    float avg = 0F;
    for (int i = 0; i < vals.length; i++)
        avg += vals[i];
    return avg /= vals.length;
}

static int indexOfMin(float[] vals) { // Returns the index of the minimum;
    int idx = -1;
    for (int i = 0; i < vals.length ; i++) {
        float v = vals[i];
        if(v>=0 && (idx==-1 || v<vals[idx])) idx=i;
    }
    return idx;
}

static float[] removeMins(int n, float[] vals) { //Removes n minimums of the array and returns a new array without them;
    while (n > 0) {
        int a = indexOfMin(vals);
        vals[a] = -1;
        --n;
    }
    float[] res = new float[vals.length - n];
    int b = 0;
    for (int i = 0; i < vals.length; i++)
        if (vals[i] != -1) {
            res[b] = vals[i];
            ++b;
        }
    return res;
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    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(); //Line of the Error;
    }
    System.out.println("Number of Grades to the Average ? ");
    int n = in.nextInt();
    float [] best = removeMins(n,vals);
    System.out.print("Best " +n+ " Grades: ");
    for (int i = 0; i < best.length ; i++)
        System.out.print(best[i]);
    System.out.println("");
    System.out.println("Average of the " +n+ " Best Grades = "+average(best));
}

}

EDIT

I'm using Intellij

Input and full error message :

Number of Grades ?

5

Grade 1 ?

7.3

Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:860)
at java.base/java.util.Scanner.next(Scanner.java:1497)
at java.base/java.util.Scanner.nextFloat(Scanner.java:2399)
at G2a.main(G2a.java:42)

Process finished with exit code 1

Rui
  • 13
  • 3
  • What input do you give your program? What's the exact and complete error message? You might want to take a look at [mcve] - there's a lot of code here that is not necessary to reproduce this specific issue you're having. – Bernhard Barker Jan 13 '18 at 21:11
  • Could you tell what IDE are you using when executing this program? – Przemysław Moskal Jan 13 '18 at 21:14
  • Probably a duplicate of [Scanner double value - InputMismatchException](https://stackoverflow.com/q/17150627) – Bernhard Barker Jan 13 '18 at 21:14

3 Answers3

2

I guess you use something like this in input:

1.5

Use the following instead:

1,5

to type floating point numbers. If you're using dots, the solution might be precising locale used in Scanner. Try changing first line in main() method from:

Scanner in = new Scanner(System.in);

to:

Scanner in = new Scanner(System.in).useLocale(Locale.US);

This line will precise what notation you want to use when typing decimal numbers. Look, how it changed the way input is being read (now dots are OK, when commas are not):

Number of Grades ? 
3
Grade 1 ? 
1.3
Grade 2 ? 
1,4
Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Unknown Source)
    at java.base/java.util.Scanner.next(Unknown Source)
    at java.base/java.util.Scanner.nextFloat(Unknown Source)
    at G2a.main(G2a.java:44)
Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21
0

I think your error is occurs because you giving input like

2.6
3.6

etc..

It's a float and input should be with comma's instead of dot's. So

2,6
3,6
etc..

I tested it and input works fine :)

// EDIT
As Dukeling mention about duplicate, if you want it to be comma's you need to use

Scanner scanner = new Scanner(System.in).useLocale(Locale.US);

More info here --> Scanner double value - InputMismatchException

Aramka
  • 111
  • 3
  • 10
0

It seems that when I tested your code, I entered the percentage as in 76 and 100 with no errors. Your problem is that the method nextInt reads only one integer, not an array. Scanner was parsing the commas as part of a string, so enter the percentages of the grades and you should be fine.