0

The result of this below only print out

John 90
Shawn 100
Melalin 75
Jane 80

What i want is

Melalin 75
Jane 80
John 90
Shawn 100

I tried all methods but still can't think of how to make it arrange from the lowest score to the highest. Can anyone help me? I am new to java. Thanks.

class students
{
public static void main(String args[]) 
    {
    String studentName[] = { "John ", "Shawn ", "Melalin ", "Jane " };
    double studentScore[] = { 90, 100, 75, 80 };
    int i;

    for(i=0; i<4; i++)
        {

        System.out.println(studentName [i] + studentScore [i]);
        }
    }
}

Thanks.

Edited by pyTony: fixed formatting

7
Contributors
10
Replies
11
Views
10 Years
Discussion Span
Last Post by masijade
0

Well, of course it does, when you are not making any effort to sort the information. First you need to equate the data in the two arrays with each other (probably hashmap) then look at java.util.Arrays to sort the score and output the info from the hashmap.

0

Were you told to implement any class infrastructure? Cos if not that's real simple.

no class, just real simple coding... im a newbie in java. SO how can i do it? Can someone code it out for me? Erm, not the easy way round... not by changing the number in ascending order...

0

no class, just real simple coding... im a newbie in java. SO how can i do it? Can someone code it out for me? Erm, not the easy way round... not by changing the number in ascending order...

Sorrie, we don't do people's homework. Try masijade's suggestion actually.

Or this one... http://www.jfind.com/articles/glass040700.shtml if you wanna go down the class route.

0
/*SORTING OF ARRAY*/
import java.util.*;

class Students
{
	public static void main(String args[])
	{
	String studentName[] = { "John ", "Shawn ", "Melalin ", "Jane " };
	double studentScore[] = { 90, 100, 75, 80 };
	//int i;
	Arrays.sort(studentName);
	for(int i=0; i<4; i++)
	{
		//Arrays.sort(studentScore);
		System.out.println(studentName [i] + studentScore [i]);
	}

	}
}

you have to just import a class named java.util.* as import java.util.* at the very beginning of your program & just write Arrays.sort(studentScore) after the definition of array.
Infact You can also sort the array according to their names.

0

@Casinoua: don't just hand out custom made code, certainly not if it contains logical errors.
the OP doesn't want to sort on the names, just on the codes. if you sort like you do, your names will be placed next to the wrong name.

0

If you check the posting dates you'll see that it was @casinoua who disinterred this corpse. @stultuske was just telling him off (quite rightly).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.