In: Computer Science
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);
}
}
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 :