I have an assignment to create 2 arrays. One holding Student names, one holding their grades. They then have to be sorted using BubbleSort, so that their grades are in descending order. I have created the first part of it, that allows me to put in their grades, and I was able to succesfully sort it using BubbleSort. I ran into a problem when I realized the professor wanted it as Doubles (I had previously had it set as Ints) and now even though I think I changed all instances of Int to Double I'm still getting an error saying found Int, expected Double.

code is as follows.

import java.util.Scanner;

public class Grades {

public static void main(String[]args){

{
Scanner UserIn = new Scanner(System.in);
System.out.print( "How many students are there? " );
double[] GradeArray = new double[UserIn.nextDouble()];

for( double i=0 ; i<GradeArray.length ; i++ ) 
{
System.out.print( "Enter Grade for Student " + (i+1) + ": " );
GradeArray[i] = UserIn.nextDouble();
}

bubbleSort(GradeArray);

for( double i : GradeArray ) System.out.println( i );
System.out.println();

}
}

private static void bubbleSort(double[]GradeArray){

double n = GradeArray.length;
double temp = 0;

for(double i=0; i<n; i++){
    for(double j=1; j<(n-i);j++){

        if(GradeArray[j-1]<GradeArray[j]){
            //swap
            temp=GradeArray[j-1];
            GradeArray[j-1]=GradeArray[j];
            GradeArray[j]=temp;
            }
        }
    }
}
}

I have not started the String array for the students names yet, as I do not know how to sort that to correspond with the BubbleSort on the grades.

Any help would be appreciated.

Recommended Answers

All 3 Replies

it seems to be right..but you can't create array with double value like 4.5 in line 10.
Instead you can store double value in array.So it shows correct error.

Create array with size of Integer Value,there is never double value for array size.

Also you can't make for loop that will run for given double variable,you can't say that your loop executes 4.7 times..so in for loop there is only integer variable you can put.

I think your professor just want that value stored in GradeArray would be double,so it will sorted using bubble sort with double value.

Just clear your question..

Thanks

you can't make for loop that will run for given double variable,you can't say that your loop executes 4.7 times..so in for loop there is only integer variable you can put.

Sorry, that's not true. There's absolutely no problem using a double for a loop variable like that, as long as you don't use == for the termination condition, just try it! Eg:
for (double d = 0; d<4.7; d+=0.5) System.out.println(d);

You are right,james...Thanks

But as it seems from the given code, it just want to sort the array having double value in ascending or descending order.

In that context i have given the answer..sorry for that....
again thanks james,as you have pointed me out of my mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.