#1 Posted by gorkamorkaorka (507 posts) -

I'm stuck on this program. I need to write a program that has you enter in the number of students, and then type in their name and score. It outputs the name and score sorted from greatest to least. You have to use single dimensional strings. I am able to read it all, store it, then sort the numbers from least to greatest, reverse that to greatest to least but I can't figure out how to attach the names to the scores.

`Enter number of students: 3  Smith 70  Jones 30  Peterson 100`
//The output is
`Peterson 100  Smith 70  Jones 30`

Here is my half finished code if any of it makes sense:

import java.util.Scanner;
public class JavaApplication46 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of students: ");

int numOfStu = input.nextInt();

String[] names = new String[numOfStu];

String name = "";

for(int i = 0; i < numOfStu; i++){
name = input.next();
names[i] = name;
System.out.println("");
}

//this is the area that sorts it from least to greatest
//i is the indexed value of the last number in array
//if it's 10 numbers big, i is 9
//loop ends before index 0 because 0 should be in it's place at the end already
for(int i = grades.length - 1; i > 0; i--){
//resets both to 0 to start at the beginning of the array
//so that you can test the new first number
int currentMaxIndex = 0;

//finds largest number out of all up to back-limit
//i is back-limit that gets chopped off by one each time
for (int k = 1; k <= i; k++){
currentMaxIndex = k;
}
}
//after largest number is found, assign that number to i
//i is a high number like 9, then 8, then 7, etc.
//each time it runs, i-- so each second highest max number
//gets put infront of the all time highest number
}

}
public static int[] reverseInt(int[] array) {
int[] poop = new int[array.length];

for(int i =0, j = poop.length -1; i < array.length; i++, j-- ) {
poop[j] = array[i];
}

return poop;
}

}

#2 Edited by zels (212 posts) -

@gorkamorkaorka:

Just do the same with the names, indeed whenever you change one array change the other so that the data in tempArray[i] always corresponds to the data stored in tempArray2[i].

EDIT:

As an alternative, you could create a class to hold all this data and just sort objects of the class. E.g.

class Student{

private String name;

name = argName;

}

//accessor functions go here

}

Then have an array of students, e.g.

Student [] class = new Student[50];

#3 Posted by nintendoeats (6144 posts) -

I remember doing this in High School. I can't help at all, but good luck.

#4 Posted by zels (212 posts) -

Try the following and lemme know if thats what you meant. The code is still a mess but it has the most vital functionality.

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

/**

*

*/

import java.util.Scanner;

class StudentList {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter number of students: ");

int numOfStu = input.nextInt();

String[] names = new String[numOfStu];

for (int i = 0; i < numOfStu; i++) {

String name = input.next();

names[i] = name;

System.out.println("");

}

//this is the area that sorts it from least to greatest

//i is the indexed value of the last number in array

//if it's 10 numbers big, i is 9

//loop ends before index 0 because 0 should be in it's place at the end already

for (int i = grades.length - 1; i > 0; i--) {

//resets both to 0 to start at the beginning of the array

//so that you can test the new first number

int currentMaxIndex = 0;

//finds largest number out of all up to back-limit

//i is back-limit that gets chopped off by one each time

for (int k = 1; k <= i; k++) {

currentMaxIndex = k;

}

}

//after largest number is found, assign that number to i

//i is a high number like 9, then 8, then 7, etc.

//each time it runs, i-- so each second highest max number

//gets put infront of the all time highest number

String tempName = names[currentMaxIndex];

names[currentMaxIndex] = names[i];

names[i] = tempName;

}

System.out.println(names[i] + " " + grades[i]);

}

public static int[] reverseInt(int[] array) {

int[] poop = new int[array.length];

for (int i = 0, j = poop.length - 1; i < array.length; i++, j--) {

poop[j] = array[i];

}

return poop;

}

public static String[] reverseString(String[] array) {

String[] poop = new String[array.length];

for (int i = 0, j = poop.length - 1; i < array.length; i++, j--) {

poop[j] = array[i];

}

return poop;

}

}

#5 Posted by gorkamorkaorka (507 posts) -

@zels:

Could you plug that in there for the first example? I hate to ask but there's a lot at stake here and there are other java related things I need to learn. I've wasted all day on this one thing.

#6 Posted by zels (212 posts) -

@gorkamorkaorka: Plug which solution in? If you'd like to see an example with the "Student" class lemme know and I'll do it for you. I gtg now, but I'll bookmark it and get back to it later today and help you if you still need it.

#7 Posted by Steve_C (1768 posts) -

I'd recommend just using a simple sorting algorithm like bubble sort (google!) on the grades array. It's simple to implement.

Since the grades and names are mapped to each other by index, whenever you swap two elements in the grade array, add a tiny bit of code to swap the elements in the name array straight after using the same indexes.

#8 Posted by Green_Incarnate (1789 posts) -

Looks like you already did it, but weren't linking the names to the grade. Easiest solution is probably do what zels did.

#9 Posted by ShadowSkill11 (1879 posts) -

I don't see why you people are doing this guys homework for him instead of just telling him to go to his teachers office hours or lab hours.

#10 Posted by Ubersmake (771 posts) -

What has to be stored as a String? The input? If that requirement is only imposed on the input, why not split up the input String for each name-grade pair, then store those values into a Map?

#11 Posted by foggel (2778 posts) -
@zels said:

poop[j] = array[i];

}

return poop;

}

public static String[] reverseString(String[] array) {

String[] poop = new String[array.length];

for (int i = 0, j = poop.length - 1; i < array.length; i++, j--) {

poop[j] = array[i];

}

return poop;

}

}

Wait, is this real?
#12 Posted by TruthTellah (9651 posts) -

@foggel said:

@zels said:

poop[j] = array[i];

}

return poop;

}

public static String[] reverseString(String[] array) {

String[] poop = new String[array.length];

for (int i = 0, j = poop.length - 1; i < array.length; i++, j--) {

poop[j] = array[i];

}

return poop;

}

}

Wait, is this real?

It is all too real.

#13 Posted by ShadowConqueror (3349 posts) -

@foggel said:

@zels said:

poop[j] = array[i];

}

return poop;

}

public static String[] reverseString(String[] array) {

String[] poop = new String[array.length];

for (int i = 0, j = poop.length - 1; i < array.length; i++, j--) {

poop[j] = array[i];

}

return poop;

}

}

Wait, is this real?

You didn't realize that programs rely on poop arrays to function properly?

#14 Edited by SSully (4723 posts) -

Damn I remember doing something exactly like this my freshman year of college. Sadly I have thrown every bit of knowledge I have of Java out the window. Good luck when you get to C++! I like it better, but it can be more difficult.

@foggel: Poop is an arbitrary name, you can switch that with cunt if you want and it would work.

#15 Posted by foggel (2778 posts) -
@TruthTellah
@ShadowConqueror: Why would they call it poop? Surely there must be a better name out there... or is it some kind of an elaborate joke?

And what does it do?
#16 Posted by selbie (2112 posts) -

So basically you have a performance-based grading system for a bunch of little turds. That sounds accurate :D

#17 Posted by TruthTellah (9651 posts) -

@foggel: It's a generic name for a variable he defined. He could have put wonderwoman for all it matters. He just knows that "poop" is supposed to be the final output information. So, while it is certainly a bit silly, it is actual code.

#18 Posted by foggel (2778 posts) -
@TruthTellah: Oh, I thought it was a part of Java or something. Thanks though.
#19 Posted by Radar (917 posts) -

Don't use two separate arrays (one for int, one for String). It just makes it messy and complicated. Make use of objects and use two sorting (ascending/descending) methods.

#20 Posted by SlasherMan (1723 posts) -

A lot of you are assuming he even knows about classes and objects, but considering what he needs helps with, I really doubt they got to that point in his course. If all you're trying to do is procedural programming, then the most straightforward way to link both arrays to reflect each change you make in one in the other, much like you did in the input loop.

Here's what your sorting loop should look like if this is what you're after:

Additions are marked with red dot. This will change the sorting in the names array as the grades array is changed. Do the same for whatever other sorting functions there are.

Also, in the future, you should consider using pastebinfor posting code. It maintains indentations and highlighting, which makes it easier for everyone else to read. Also, there are websites made for this sort of thing. Seeking advice on GB for this is not the best idea.