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.
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!