Help with simple percentage problem

Hello again :)

I have to make a program that accepts three names, their roll numbers and percentages then prints out in the following fashion , along with rank(sorted by percentage):

Roll noName PercentageRank

1BLA991

3BLAA 952

2ABC87 3

This is my code, but my output is not correct, i dont know why:

import java.io.*;

publicclass Percentage

{

publicvoid display()throws IOException

{

InputStreamReader isr =new InputStreamReader(System.in);

BufferedReader br =new BufferedReader(isr);

String name[]=new String[3];

int roll[]=newint[3];

int percent[]=newint[3];

int rank[]=newint[3];

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

{

System.out.println("Enter name");

name[i]=br.readLine();

System.out.println("Enter roll number");

roll[i]=Integer.parseInt(br.readLine());

System.out.println("Enter percentage");

percent[i]=Integer.parseInt(br.readLine());

}

sort(roll,percent,name);

print(roll,percent,name);

}

privatevoid sort(int roll[],int percent[],String name[])

{

for (int i=1;i<percent.length-1;i++)

{

for (int j=i+1;j<percent.length;j++)

{

if (percent[i]>percent[j])//change to roll[i]>roll[j] to sort by roll number. Current sorting by percentage

{

int temp=roll[i];

roll[i]=roll[j];

roll[j]=temp;

String tempo=name[i];

name[i]=name[j];

name[j]=tempo;

int temp1=percent[i];

percent[i]=percent[j];

percent[j]=temp1;

}

}

}

}

privatevoid print(int roll[],int percent[],String name[])

{

System.out.println("Roll Number\tName\tPercentage\tRank");

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

{

System.out.println(roll[i]+"\t"+name[i]+"\t"+percent[i]+"\t"+(i+1));

}

}

}

>

[3831 byte] By [Overkilla] at [2007-11-27 10:22:08]
# 1

I recommend that you create a new class that implements the comparable interface and has fields for name, int, and float. The compareTo routine would sort based on percent (you could make name a secondary sort item).

Then in another class, create of list of these objects and sort them using Collections.sort() or some such. It would be easy and instructive to create this.

Anyone, please correct any mistakes made. Thanks.

petes1234a at 2007-7-28 17:14:09 > top of Java-index,Java Essentials,Java Programming...
# 2

We aren't allowed to use interface and implementing, and haven't learnt about the Collections class.

I would appreciate it if you could tell me what is wrong with my logic in the code that is causing this problem , thanks :).

Overkilla at 2007-7-28 17:14:09 > top of Java-index,Java Essentials,Java Programming...
# 3

>for (int i=1;i<percent.length-1;i++)

>{

> for (int j=i+1;j<percent.length;j++)

>{

Shouldn't the first for loop start with i = 0 ?

aniseeda at 2007-7-28 17:14:09 > top of Java-index,Java Essentials,Java Programming...
# 4

... although that will sort in ascending order of percentage, not what you wanted!

aniseeda at 2007-7-28 17:14:09 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks for the help.. doh stupid mistake

I can sort out ascending to descending myself. (I cant expect to pass any sort of exam if i cant do that hehe)-percent<percent[j]) should do it :D>

Overkilla at 2007-7-28 17:14:09 > top of Java-index,Java Essentials,Java Programming...