How to create a String Array and associate it with a Grade array

I have a project that I need to create 2 arrays, one for storing student names and one for holding student points. The user enters the size of the array, and the array must be sorted using BubbleSort (by placing the upper bounds at the top). I started the project, created the first array for the evaluation, I successfully sorted the bubbles and sorted the estimates. Now I can’t figure out how to create an array for names, and as soon as I do, how to make an array of names match the Grades BubbleSort array?

Here is the code I have.

import java.util.Scanner; public class Grades { public static void main(String[]args){ { Scanner GradeIn = new Scanner(System.in); Scanner NameIn = new Scanner(System.in); System.out.print( "How many students are there? " ); int[]GradeArray = new int[GradeIn.nextInt()]; String[]nameArray = new String[GradeIn.nextInt()]; for( int i=0 ; i<GradeArray.length ; i++ ) { System.out.print( "Enter Grade for Student " + (i+1) + ": " ); GradeArray[i] = GradeIn.nextInt(); System.out.print( "Enter Name of Student " + (i+1) + ": " ); nameArray[i] = NameIn.nextLine(); } bubbleSort(GradeArray, nameArray); for( int i : GradeArray ) System.out.println( i ); System.out.println(); } } private static void bubbleSort(int[]GradeArray, String[] nameArray){ int n = GradeArray.length; int temp = 0; String temp2; for(int i=0; i<n; i++){ for(int j=1; j<(ni);j++){ if(GradeArray[j-1]<GradeArray[j]){ //swap temp=GradeArray[j-1]; GradeArray[j-1]=GradeArray[j]; GradeArray[j]=temp; temp2=nameArray[j-1]; nameArray[j=1]=nameArray[j]; nameArray[j]=temp2; } } } } } 

Also, how do I change grades to Double? I started with Int, and when I try to change everything to double, I get the error "Found Double, expected Int".

What the professor asks for: Write a program in which the user enters the number of students, the names of students and their scores and prints the names in descending order in accordance with their ratings.

ADDITIONAL INFORMATION:

You will need two arrays. One to keep the lines. Another to conduct student grades. (Doubles)

The size of the arrays will be entered by the user.

You will have to sort the arrays in the main () method. I recommend using BubbleSort (http://www.java-examples.com/java-bubble-sort-example), but not as a separate method. TIP. When sorting an array of grades, you need to sort the array of names according to the estimates.

And finally, you must enable the (void printAnswer (String [] names)) method to print the array of names after sorting it.

+4
source share
1 answer

Create your own object. This is the whole idea of ​​object-oriented programming. You do not have a name and class , you have Student who has a name and class. Then you manipulate the student in any way that suits you.

 public class Student { private String name; private int grade; public Student(String name, int grade) { this.name = name; this.grade = grade; } public String getName() { return name; } public int getGrade() { return grade; } } 

Then when you have

 int[]GradeArray = new int[UserIn.nextInt()]; 

Do this instead:

 Student[] studentArray = new Student[UserIn.nextInt()]; 

I leave the rest of the changes as an exercise so that you get used to how it should work. Although, remember that Student[] filled with null when you read the data, you need to create a new Student(name, grade) each time.


Alternative solution

Above was the right way to do this. Don't learn anything from the answer below, because it defeats the whole purpose of learning Java. I would send a letter to the professor if this decision is acceptable.

At the same time, as in every swap class, you can make exactly the same exchange in the String name array, i.e.

 // change method signature private static void bubbleSort(int[] gradeArray, String[] nameArray){ int n = gradeArray.length; int temp = 0; String temp2; for(int i=0; i<n; i++){ for(int j=1; j<(ni);j++){ if(gradeArray[j-1]<gradeArray[j]){ //swap temp=gradeArray[j-1]; gradeArray[j-1]=gradeArray[j]; gradeArray[j]=temp; // New code begin temp2=nameArray[j-1]; nameArray[j-1]=nameArray[j]; nameArray[j]=temp2; // New code end } } } } 

Again, DO NOT DO IT IN THE REAL WORLD WHEN YOU DO NOT NEED AT SCHOOL . It is MORE LEFT and much more confusing.

+2
source

All Articles