# Sorting Arrays

• November 26th, 2011, 11:59 PM
Bryan29
Sorting Arrays
Hello,

I'm a beginner in Java and am having trouble putting this program exercise together / I think I got it and I may have just messed up somewhere along the lines.

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

This is what I have so far.

Code Java:

```  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();//grab the string input values and put into the name array } int[] score = new int[numberOfStudents]; for (int i = 0; i < score.length; i++) { score[i] = input.nextInt();//grab the integer input values and put them in the score array } decreasingOrder(name, score); }   public static void decreasingOrder(String[] name, int[] score) {   for (int i = 0; i < score.length - 1; i++) {   int currentMin = score[i]; int currentMinIndex = i; String currentMinName = name[i];     for (int j = i + 1; j < score.length; j++) { if (currentMin > score[j]) { currentMin = score[j]; currentMinIndex = j; currentMinName = name[j];//swap the string array values } }     if (currentMinIndex != i) { score[currentMinIndex] = score[i]; score[i] = currentMin; } System.out.println(name[i] + score[i]); }   } }```

I'm trying to make it so when I input my first String value, it goes to String index 0, and when I input my first Integer value, it goes to Integer index 0, so they match and I can swap them by their indexes. Is this the right way to approach this program?

The method is also where I get really confused, I'm not sure how to swap the name that goes with the integer inputed with it.
This is what I have so far.

Any kind of help/hints/tips/explanations is greatly appreciated :)

Thanks!
• November 27th, 2011, 01:19 AM
that_guy
Re: Sorting Arrays
First of all your program doesnt ask for user input when inputing the students names and scores. It just expects the user to know what the program wants.

Your first for loop in decreasingOrder() has score.length - 1 ... what is the minus 1 for?

I suggest using if else statements to compare the scores of each student. If one score is higher than the other than do a quick exchange between them and then dont forget to swap the names as well. So compare score[0] to score[1], if index 0 is higher then it stays where it is, if not then it swaps with index 1. After that do score[1] compares to score[2], etc.

to do a quick swap do the following idea:

Code java:

```for(i = 0;i < score.length;i++) { if(score[i] < score[i + 1]) { int swap = score[i]; score[i] = score[i + 1]; score[i + 1] = swap; } }```
• November 27th, 2011, 10:09 AM
Bryan29
Re: Sorting Arrays
Thanks a lot for the help and explanation :) The input is like that because it needs to look a certain way in order to go through as correct on his website where we turn these in.

I switched it but I still get this error that I got before I switched it:

Enter number of Students: 3
Smith
70
Jones
30
at java.util.Scanner.throwFor(Scanner.java:841)
at java.util.Scanner.next(Scanner.java:1464)
at java.util.Scanner.nextInt(Scanner.java:2108)
at java.util.Scanner.nextInt(Scanner.java:2067)
at Exercise06_19.main(Exercise06_19.java:17)
• November 27th, 2011, 01:13 PM
Norm
Re: Sorting Arrays
InputMismatchException means that you entered the wrong type of data.
The nextInt method on line 17 wants numeric data.
• November 28th, 2011, 12:17 AM
that_guy
Re: Sorting Arrays
Yes, what Norm said. Your program expects you to enter all 3 of the names first then all three of the scores.

your program wants the following input:
Smith
Jones
Peterson
70
30
100

To fix this either combine the for loops into one loop or have the computer prompt the user for the info so there is no confusion.
• November 28th, 2011, 06:21 AM
Mr.777
Re: Sorting Arrays
Advise: I will recommend you to use input.nextLine() instead of input.next(). If you don't know the difference, you must know it.