Question

In: Computer Science

Write a program that prompts the user to enter the number of students and each student’s...

Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score and the student with the second-highest score. Use the next() method in the Scanner class to read a name rather using the nextLine() method.
This is my code , but i get this error Enter the number of students: Exception in thread "main" java.util.InputMismatchException
   at java.util.Scanner.throwFor(Scanner.java:871)
   at java.util.Scanner.next(Scanner.java:1494)
   at java.util.Scanner.nextInt(Scanner.java:2139)
   at java.util.Scanner.nextInt(Scanner.java:2095)
   at Exercise05_09.main(Exercise05_09.java:16)

import java.util.Scanner;

public class Exercise05_09 {
   public static void main(String[] args) {
       // Create a Scanner
       Scanner input = new Scanner(System.in);

       // Prompt the user to enter the number of students
       System.out.print("Enter the number of students: ");
       int numberOfStudents = input.nextInt();

       int score,                    // Holds students' score      
           highest = 0,            // Highest score
           secondHigest = 0;   // Second highest score
       String name = "",        // Holds students' name
               student1 = "",    // Highest scoring student name
               student2 = "";   // Second highest scoring student name
      
       // Prompt the user to enter each students' name and score
       System.out.println("Enter each students' name and score:");
       for (int i = 0; i < numberOfStudents; i++) {
           System.out.print(
               "Student: " + (i + 1) + "\n Name: ");
           name = input.next();
           System.out.print(" Score: ");
           score = input.nextInt();

           if (i == 0) {
               // Make the first student the highest scoring student so far
               highest = score;
               student1 = name;
           }
           else if (i == 1 && score > highest) {
               // Second student entered scored
               // higher than first student
               secondHigest = highest;
               highest = score;
               student2 = student1;
               student1 = name;
           }
           else if (i == 1) {
               // Second student entered scored
               // lower than first student
               secondHigest = score;
               student2 = name;
           }      
           else if (i > 1 && score > highest && score > secondHigest) {
               // Last student entered has the highest score
               secondHigest = highest;
               student2 = student1;
               highest = score;
               student1 = name;
           }
           else if (i > 1 && score > secondHigest) {
               // Last student entered has the second highest score
               student2 = name;
               secondHigest = score;
           }
       }

       // Display the student with the hightest score
       // and the student with the second-hightest score.
       System.out.println(
           "Higest scoring student: " + student1 +
           "\nSecond Higest scoring student: " + student2);
   }
}

Solutions

Expert Solution

Exception in thread "main" java.util.InputMismatchException

This runtime error indicate that there is input mismatch this exception thrown when data type of variable to hold input and data type of input is not matched.

consider the following code line

score = input.nextInt();

here if keyborad input is intger type then there will be no error and if keyborad input is non integer (decimal value) say 45.8 then program get terminated with runtime error because nextInt() throws InputMismatchException if next token is not an Integer.

To resolve the above issue declare variables score, highest, secondHighest as double

and use nextDouble() instead of nextInt() as follow :

score = input.nextDouble();

by using nextDouble we can get integer input and non integer (decimal value) input as well without InputMismatchException.

Currected Code : please refer to the corrected code below

import java.util.Scanner;

public class Exercise05_09 {
   public static void main(String[] args) {
       // Create a Scanner
       Scanner input = new Scanner(System.in);

       // Prompt the user to enter the number of students
       System.out.print("Enter the number of students: ");
       int numberOfStudents = input.nextInt();

       // declare score, highest, secondHigest as double/ 
       double score,                    // Holds students' score      
           highest = 0,            // Highest score
           secondHigest = 0;   // Second highest score
       String name = "",        // Holds students' name
               student1 = "",    // Highest scoring student name
               student2 = "";   // Second highest scoring student name
      
       // Prompt the user to enter each students' name and score
       System.out.println("Enter each students' name and score:");
       for (int i = 0; i < numberOfStudents; i++) {
           System.out.print(
               "Student: " + (i + 1) + "\n Name: ");
           name = input.next();
           System.out.print(" Score: ");
           //score = input.nextInt(); //change this line to below
           score = input.nextDouble();

           if (i == 0) {
               // Make the first student the highest scoring student so far
               highest = score;
               student1 = name;
           }
           else if (i == 1 && score > highest) {
               // Second student entered scored
               // higher than first student
               secondHigest = highest;
               highest = score;
               student2 = student1;
               student1 = name;
           }
           else if (i == 1) {
               // Second student entered scored
               // lower than first student
               secondHigest = score;
               student2 = name;
           }      
           else if (i > 1 && score > highest && score > secondHigest) {
               // Last student entered has the highest score
               secondHigest = highest;
               student2 = student1;
               highest = score;
               student1 = name;
           }
           else if (i > 1 && score > secondHigest) {
               // Last student entered has the second highest score
               student2 = name;
               secondHigest = score;
           }
       }

       // Display the student with the hightest score
       // and the student with the second-hightest score.
       System.out.println(
           "Higest scoring student: " + student1 +
           "\nSecond Higest scoring student: " + student2);
   }
}

Sample Output :

Please refer to the below Screenshot of the code to understand indentation of the code :


Related Solutions

IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Write a program that prompts the user to enter a series of strings, but with each...
Write a program that prompts the user to enter a series of strings, but with each string containing a small integer. Use a while loop and stop the loop when the user enters a zero. When the loop has finished, the program should display: the number of user inputs (not counting the final zero input). the total of the integers in the strings entered. the average of the integers accurate to one decimal place. Any help is greatly appreciated, this...
Write a program that prompts the user to enter a number within the range of 1...
Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive • The program then generates a random number using the random class: o If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number o If the users guess matches the random number tell them they win! o If the users guess does not match the random number tell them they...
Write a program that prompts the user to enter a number within the range of 1...
Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive • The program then generates a random number using the random class: o If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number o If the users guess matches the random number tell them they win! o If the users guess does not match the random number tell them they...
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
JAVA Write a program that prompts the user to enter a matrix number of rows and...
JAVA Write a program that prompts the user to enter a matrix number of rows and number of columns. In main method create 2D matrix based on the number of rows and columns input by the user; randomly fills the matrix with 0s and 1s and prints it. Create method sumColumns that takes only the matrix you created in main method and find the sum of each column with even index and prints it. Do not use global variables. Here...
C++ [2] Write a program that prompts the user to enter a non-negative decimal number and...
C++ [2] Write a program that prompts the user to enter a non-negative decimal number and a base in the range 2 <= base <= 16. Write a function multibaseOutput() that displays the number in the specified base. The program terminates when the user enters a number of 0 and a base 0. Run: Enter a non-negative decimal number and base (2 <= B <= 16) or 0 0 to terminate: 155 16     155 base 16 is 9B Enter...
JAVA posted multiple times Write a program that prompts the user to enter a matrix number...
JAVA posted multiple times Write a program that prompts the user to enter a matrix number of rows and number of columns. In main method create 2D matrix based on the number of rows and columns input by the user; randomly fills the matrix with 0s and 1s and prints it. Create method sumColumns that takes only the matrix you created in main method and find the sum of each column with even index and prints it. Do not use...
JAVA posted multiple times Write a program that prompts the user to enter a matrix number...
JAVA posted multiple times Write a program that prompts the user to enter a matrix number of rows and number of columns. In main method create 2D matrix based on the number of rows and columns input by the user; randomly fills the matrix with 0s and 1s and prints it. Create method sumColumns that takes only the matrix you created in main method and find the sum of each column with even index and prints it. Do not use...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT