Hello,
I posted this program last night and got help with the sorting of the arrays, but I still get an error whenever I try and run it. There is something wrong with the input portion of my code in the main and I'm not sure what it is.
The program is to prompt the user for the number of students, the student names, and their scores. It should then print them out in decreasing order of their scores.
Output should be:
Enter number of students: 3
Smith 70
Jones 30
Peterson 100
The print out is
Jones 30
Smith 70
Peterson 100
However, my output / print out is coming out as:
Enter number of Students: 3 Smith 70 Jones 30 Mary 100
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Exercise06_19.main(Exercise06_19.java:18)
Java Result: 1
import java.util.Scanner; public class Exercise06_19 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter number of Students: "); int numberOfStudents = input.nextInt(); String[] name = new String[numberOfStudents]; for (int i = 0; i < name.length; i++) { name[i] = input.next(); } int[] score = new int[numberOfStudents]; for (int i = 0; i < score.length; i++) { score[i] = input.nextInt(); } decreasingOrder(name, score); } public static void decreasingOrder(String[] name, int[] score) { for (int i = 0; i < score.length; i++) { if (score[i] < score[i + 1]) { int swap = score[i]; String swap2 = name[i]; score[i] = score[i + 1]; score[i + 1] = swap; name[i] = name[i + 1]; name[i + 1] = swap2; } System.out.println(name[i] + score[i]); } } }
Any kind of help/guidance would be great Thanks!