0

This is my code:

package gradecalculator;

import java.util.Scanner;

public class GradeCalculator 
{
    public static void main(String[] args) 
    {
        String categoryName = null;
        double categoryGrade;
        double categoryWorth;
        double Grade = 0;

        String[] nameArray = new String[10];
        double[] gradeArray = new double[10];
        double[] worthArray = new double[10];
        double[] categoryArray = new double[10];

        int c = 0;
        int c1 = 0;

        Scanner entry = new Scanner(System.in);

        while(!(categoryName = entry.next()).equals("Quit"))
        {            
            categoryGrade = entry.nextDouble();
            gradeArray[c] = categoryGrade;

            categoryWorth = entry.nextDouble();
            worthArray[c] = categoryWorth;

            categoryName = entry.nextLine();
            nameArray[c] = categoryName;

            categoryArray[c] = finalCategory(categoryGrade, categoryWorth);

            c++;
        }

        while(c != 0)
        {
            System.out.printf("%S:\t%f%%\tWorth: %f\n", nameArray[c1], gradeArray[c1], worthArray[c1]);
            c--;
            c1++;
        }

        while(c1 != 0)
        {
            Grade = Grade + categoryArray[c];
            c1--;
            c++;
        }
    }

    public static double finalCategory(double Grade, double Worth)
    {
        return (Grade * (Worth / 100));
    }
}

The error occurs as soon as I enter the 3rd input (categoryName) - it appears is at line 29:

categoryWorth = entry.nextDouble();

Error:

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 gradecalculator.GradeCalculator.main(GradeCalculator.java:26)
Java Result: 1

I am not sure what is causing the error, considering the line that Netbeans is giving me has nothing to do with the string input. If anyone knows how I can fix this, I would greatly appreciate it. Thanks.

CoolKat
  • 112
  • 1
  • 12
  • If it's a nextDouble problem, I don't think it'd be associated with categoryName. Maybe it's breaking on the second iteration of the while loop? – DaaaahWhoosh Dec 13 '14 at 05:04
  • 1
    Try adding `entry.nextLine()` before that. – Spikatrix Dec 13 '14 at 05:05
  • After you retrieve your first `nextDouble` there is/are end-of-line charcter(s) left in your keyboard buffer. Your second `nextDouble` chokes on them. – PM 77-1 Dec 13 '14 at 05:05
  • possible duplicate of [Scanner double value - InputMismatchException](http://stackoverflow.com/questions/17150627/scanner-double-value-inputmismatchexception) – PM 77-1 Dec 13 '14 at 05:12

0 Answers0