0

Been looking for about an hour now, do not understand where I'm going wrong.

From what I understand it's Scanner looking for an integer but finding a String instead that is causing the issue?

I'm very new to coding but from what I can make out the issue starts here:

public void inputGrades()
{
    Scanner input = new Scanner(System.in);

    int grade;

    System.out.printf("%s\n%s\n %s\n %s\n",
            "Enter the integer grades in the range 0-100.",
            "Type the end-of-file indicator to terminate input:",
            "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
            "On Windows type <ctrl> z then press Enter");

    while( input.hasNext())
    {
        grade = input.nextInt();
        total += grade;
        ++gradeCounter;

        incrementLetterGradeCounter(grade);
    }
}

I'm following one of Paul Deitel's books on my college course and I'm completely stumped!

Any help is much appreciated!

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75

4 Answers4

0

You can use Integer.parseInt() to convert the string to integer. But Does your printf statement stops getting input when you press Ctrl+Z?

Dora
  • 191
  • 8
0

I found something that might work for you. Follow those steps (code sample below):

  1. Initialize your grade variable to something (usually 0 or 1):

    int grade = 0;

  2. Use next() instead of nextInt() so that you can read any input:

    String nextInput = input.next();

  3. Parse your input with Integer.parseInt(), converting your input into a number:

    grade = Integer.parseInt(nextInput);

  4. If your user types a String, it will throw an exception and end your program. You may surround your grade parsing with an try/catch. In your case, the exception thrown would be a NumberFormatException:

    try { grade = Integer.parseInt(nextInput); System.out.println(grade); } catch (NumberFormatException e) { System.out.println("Mauvais format de nombre, veuillez recommencer"); }

  5. Profit ! Below is your full method.

    Scanner input = new Scanner(System.in); int grade = 0;

        System.out.printf("%s\n%s\n %s\n %s\n",
                "Enter the integer grades in the range 0-100.",
                "Type the end-of-file indicator to terminate input:",
                "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
                "On Windows type <ctrl> z then press Enter");
    
        while( input.hasNext())
        {
            String nextInput = input.next();
    
            try {
                grade = Integer.parseInt(nextInput);
                System.out.println(grade);
            } catch (NumberFormatException e)   {
                System.out.println("Mauvais format de nombre, veuillez recommencer");
            }
        }
    

    ( Sorry for the poor formatting, I had a rough time with the layout. )

Yassine Badache
  • 1,810
  • 1
  • 19
  • 38
0

You should use matching has/next methods. Scanner.hasNext() matches Scanner.next(), and Scanner.hasNextInt() matches Scanner.nextInt(). If your input contains a token that is not a valid integer, then hasNextInt() will return false while hasNext() will return true. You can use that to your advantage as shown below.

Also, it's good practice to keep variables to the smallest possible scope; therefore I've moved the declaration of grade to inside the loop.

And finally, for portability, use %n instead of \n in your printf calls.

public void inputGrades()
{
    Scanner input = new Scanner(System.in);

    System.out.printf("%s%n%s%n %s%n %s%n",
            "Enter the integer grades in the range 0-100.",
            "Type the end-of-file indicator to terminate input:",
            "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
            "On Windows type <ctrl> z then press Enter");

    while (input.hasNextInt())
    {
        int grade = input.nextInt();
        total += grade;
        ++gradeCounter;

        incrementLetterGradeCounter(grade);
    }

    if (input.hasNext()) {
        System.out.printf(
                "Fatal error: the token entered as  \"%s\" is not a valid integer.%n",
                input.next());
        System.exit(1);
    }
}
DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42
0

Right, I feel like a completed idiot.

Thank you all for your suggestions and I will take them on board and hopefully utilise them soon in my programming!

As it turns out, following Paul Deitel, he uses the command prompt to run his code and his way of showing an end-of-file indicator is by pressing Ctrl+Z, this had been labelled in one of the presentations as both + z as well as ^Z.

When I have tried to input either of those, that is where the error of a string rather than an integer has come up...

Thank you to those who tried to find me an answer!