1

***Updating my post to show all of my code, hopefully this provides more context.

When I run the program, it skips through my first "if" statement and throws an error.***

I am getting the error Exception in thread "main" java.lang.NumberFormatException: For input string: ""

I'm trying to convert a string variable to an int within the first else statement of a while loop...The error occurs at int examScore = Integer.parseInt(userInput).

   import java.util.Scanner;//imports scanner

   public class ExamScoresCalculator{

   public static void main(String[] args){



   Scanner scan = new Scanner(System.in);//creates new scanner object 

   //prompts user for lowest possible exam score
   System.out.println("Enter the lowest possible value for the exam: ");
   int lowRange = scan.nextInt();

   //prompts user for highest possible exam score
   System.out.println("Enter the highest possible value for the exam: ");
   int highRange = scan.nextInt();

   boolean flag = true; //while loop flag
   double lowestExamScore = highRange; // holds lowest score
   double highestExamScore = lowRange; //holds highest score
   double totalPoints = 0; //holds sum of all scores entered
   int totalExams = 0; //holds the total number of exams entered by the user

   while (flag == true){
      System.out.println("Enter an exam score between " + lowRange + " and " + highRange + " or type exit. "); //asks user for an exam score within correct range     
      String userInput = scan.nextLine();

      if ((userInput.equals("exit")) || (userInput.equals("Exit")) || (userInput.equals("EXIT"))){ //checks if user enters "exit"
         flag = false; //ends while loop

      }else{      
         int examScore = Integer.parseInt(userInput); //converts user input into integer 

         if (examScore >= lowRange && examScore <= highRange){ //checks if user entered a correct test score
            totalExams += 1; //increments totalExams by 1
            totalPoints += examScore; //adds examScore total to totalPoints

            if (examScore < lowestExamScore){ //checks if the exam score entered is the lowest 
               lowestExamScore = examScore; //updates lowestExamScore variable 
            }

            else if (examScore > highestExamScore){ //checks if exam score entered is the highest 
               highestExamScore = examScore; //updates highestExamScore variable 
            }

         }else{
            System.out.println("Please enter a correct score."); //asks user to enter a correct score 
         }

      }


   }//closing while loop 

   //prints the total number of exams, lowest score, highest score, and average score to screen 
   System.out.println("Total number of exams: " + totalExams);
   System.out.println("Lowest exam score: " + lowestExamScore);
   System.out.println("Highest exam score: " + highestExamScore);
   System.out.println("Average exam score: " + (totalPoints / totalExams)); 

   }//closing main 
   }//closing class


7
  • if you give any other value than digits then you will get this error since you are converting into an integer right? Commented Oct 20, 2019 at 2:34
  • 2
    Unrelated, but while (flag = true) doesn't do what you want it to do. Commented Oct 20, 2019 at 2:35
  • 1
    Also unrelated, you don't handle "eXit", "eXIt", "eXIT" or "ExIT" (and a few more permutations!) if (userInput.equalsIgnoreCase("exit")) Commented Oct 20, 2019 at 2:40
  • 1
    You have to provide input on the command line. If you just press enter it's going to be "" (empty string) and you're going to get the error (Exception). Integer.parseInt needs to be given an integer in the form of a string, e.g. "42". Commented Oct 20, 2019 at 2:40
  • I understand that I need to provide input on the command line, but when I run the program it immediately throws the error. It seems like it skips through the "if" and goes straight into the "else" statement... Commented Oct 20, 2019 at 2:52

3 Answers 3

0

In as soon as you press ENTER to enter the highest score, the scanner reads the integer(nextInt) but also reads an end of line(nextLine) . Since in the line there is no text provided, it is read as and empty string "".

You should replace scanner.nextInt with scanner.nextLine in the code.It should work fine with below changes.

//prompts user for lowest possible exam score
  System.out.println("Enter the lowest possible value for the exam: ");
   int lowRange = Integer.parseInt(scan.nextLine());

   //prompts user for highest possible exam score
System.out.println("Enter the highest possible value for the exam: ");
   int highRange = Integer.parseInt(scan.nextLine());
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the explanation. This makes a lot more sense to me.
0

Please use two inputs for userInput and examScore which will resolve your issue since you can't use a single value for both String and Integer datatypes.

(but it is possible when your input value is '123' (digits only) even this is also not applicable for your program)

2 Comments

This answer is incorrect. It is possible to use a single value for both String and Integer datatypes
@CocoNess, It seems the question description updated after my answer. anyhow, could you please explain how we can use a single input value as both String and Integer. (please consider the quoted paragraph in my section too)
0

Add the scan.nextLine() to move the scanner to read the next line (new line).

System.out.println("Enter the highest possible value for the exam: ");
int highRange = scan.nextInt();
scan.nextLine();   // Add this line 

3 Comments

The first two scan.nextLine() inputs to take in the highest and lowest possible scores work fine. String userInput = nextLine() is throwing an error when I try to convert it to an int by using int examScore = Integer.parseInt(userInput)
I copied the code into my IDE and it works correctly when scan.nextLine() is added after the highRange input.
nextInt doesnt advance the Scanner to the next line. First the Scanner must be advanced to the next line, then you can use nextLine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.