Java: Enter number of students, enter grades and names, sort them

Avatar image for deactivated-5f90eabee6bba
deactivated-5f90eabee6bba

584

Forum Posts

415

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

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();

int[] grades = new int[numOfStu];
String[] names = new String[numOfStu];

int grade = 0;
String name = "";

for(int i = 0; i < numOfStu; i++){
name = input.next();
grade = input.nextInt();
names[i] = name;
grades[i] = grade;
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 currentMax = grades[0];
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++){
if (currentMax < grades[k]){
currentMax = grades[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
grades[currentMaxIndex] = grades[i];
grades[i] = currentMax;
}

}
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;
}

}

Avatar image for zels
zels

213

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#2  Edited By zels

@gorkamorkaorka:

grades[currentMaxIndex] = grades[i];

grades[i] = currentMax;

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;

private int grade;

Student(String argName, int argGrade){

name = argName;

grade = argGrade;

}

//accessor functions go here

}

Then have an array of students, e.g.

Student [] class = new Student[50];

Avatar image for nintendoeats
nintendoeats

6234

Forum Posts

828

Wiki Points

0

Followers

Reviews: 4

User Lists: 9

#3  Edited By nintendoeats

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

Avatar image for zels
zels

213

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#4  Edited By zels

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.

*/

/**

*

* @author admin

*/

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();

int[] grades = new int[numOfStu];

String[] names = new String[numOfStu];

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

String name = input.next();

int grade = input.nextInt();

names[i] = name;

grades[i] = grade;

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 currentMax = grades[0];

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++) {

if (currentMax < grades[k]) {

currentMax = grades[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

grades[currentMaxIndex] = grades[i];

grades[i] = currentMax;

String tempName = names[currentMaxIndex];

names[currentMaxIndex] = names[i];

names[i] = tempName;

}

for(int i=0; i<grades.length; i++)

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;

}

}

Avatar image for deactivated-5f90eabee6bba
deactivated-5f90eabee6bba

584

Forum Posts

415

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

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

Avatar image for zels
zels

213

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#6  Edited By zels

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

Avatar image for steve_c
Steve_C

1768

Forum Posts

1897

Wiki Points

0

Followers

Reviews: 0

User Lists: 5

#7  Edited By Steve_C

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.

Avatar image for green_incarnate
Green_Incarnate

1789

Forum Posts

124

Wiki Points

0

Followers

Reviews: 0

User Lists: 7

#8  Edited By Green_Incarnate

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

Avatar image for shadowskill11
ShadowSkill11

1877

Forum Posts

48

Wiki Points

0

Followers

Reviews: 0

User Lists: 5

#9  Edited By ShadowSkill11

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.

Avatar image for deactivated-5ff27cb4e1513
deactivated-5ff27cb4e1513

771

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

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?

Avatar image for foggel
foggel

2780

Forum Posts

531

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#11  Edited By foggel
@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?
Avatar image for truthtellah
TruthTellah

9827

Forum Posts

423

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#12  Edited By TruthTellah

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

Avatar image for shadowconqueror
ShadowConqueror

3413

Forum Posts

1275

Wiki Points

0

Followers

Reviews: 1

User Lists: 1

#13  Edited By ShadowConqueror

@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?

Avatar image for ssully
SSully

5753

Forum Posts

315

Wiki Points

0

Followers

Reviews: 0

User Lists: 3

#14  Edited By SSully

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.

Avatar image for foggel
foggel

2780

Forum Posts

531

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#15  Edited By foggel
@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?
Avatar image for selbie
selbie

2602

Forum Posts

6468

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#16  Edited By selbie

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

Avatar image for truthtellah
TruthTellah

9827

Forum Posts

423

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#17  Edited By TruthTellah

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

Avatar image for foggel
foggel

2780

Forum Posts

531

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#18  Edited By foggel
@TruthTellah: Oh, I thought it was a part of Java or something. Thanks though.
Avatar image for radar
Radar

933

Forum Posts

334

Wiki Points

0

Followers

Reviews: 0

User Lists: 11

#19  Edited By Radar

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.

Avatar image for slasherman
SlasherMan

1723

Forum Posts

53

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#20  Edited By SlasherMan

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.