Search code examples
javaif-statementbooleanjava.util.scannerparseint

Error when trying to convert string to integer using Integer.parseInt()


***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



Solution

  • 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());