13 Replies - 5350 Views - Last Post: 27 November 2011 - 05:56 PM Rate Topic: -----

#1 Epic800  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-August 11

Sorting Students?

Posted 27 November 2011 - 04:10 PM

Write a program that prompts the user to enter the number of students, the students' names, and their scores, and prints the students names in decreasing order of their scores?

PROBLEM: I created a bubble sort in order to put in names in decreasing order and that is not working. Also I do not believe I need to use a string for the name. Is their an easier way?

import java.util.Scanner;

public class Exercise06_19 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        
        
        System.out.print("Enter the number of students: ");
        int numberOfStudents = input.nextInt();

        int[] scores = new int[numberOfStudents];

        String[] names = new String[numberOfStudents];

        for (int i = 0; i < numberOfStudents; i++) {
            System.out.println("Enter the student's name: ");
            names[i] = input.next();
            System.out.println("Enter the student's score: ");
            scores[i] = input.nextInt();
        
        
        System.out.println(names[i] + " has a score of " + scores[i]);


    }
    }

    public static void bubbleSort(int[] a, String[] n) {
        boolean changed = true;
        do {
            changed = false;
            for (int j = 0; j < a.length - 1; j++) {
                if (a[j] > a[j + 1]) {

                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                    changed = true;
                }
            }
        } while (changed);
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Students?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: Sorting Students?

Posted 27 November 2011 - 04:13 PM

This is one of the problems with parallel arrays. You are sorting only one of the arrays, not swapping the corresponding elements in the other. Really, the better practice would be to design a Student class and manage a Student[]. If your Student class implements the Comparable interface, you can make use of the Arrays.sort() method.
Was This Post Helpful? 0
  • +
  • -

#3 Epic800  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-August 11

Re: Sorting Students?

Posted 27 November 2011 - 04:17 PM

View Postmacosxnerd101, on 27 November 2011 - 04:13 PM, said:

This is one of the problems with parallel arrays. You are sorting only one of the arrays, not swapping the corresponding elements in the other. Really, the better practice would be to design a Student class and manage a Student[]. If your Student class implements the Comparable interface, you can make use of the Arrays.sort() method.


How would I go about sorting both the arrays?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Sorting Students?

Posted 27 November 2011 - 04:19 PM

This problem begs for a Student class that contains fields "name" and "score." Does that sound at all familiar?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: Sorting Students?

Posted 27 November 2011 - 04:19 PM

Quote

This is one of the problems with parallel arrays. You are sorting only one of the arrays, not swapping the corresponding elements in the other.

From my last post.

Really though, you should avoid parallel arrays in favor of Object-Oriented programming, which adheres to standards and is a lot cleaner to work with.
Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Sorting Students?

Posted 27 November 2011 - 04:28 PM

Quote

How would I go about sorting both the arrays?


You've completely neglected to swap the names in n when you swap integers in a.
Was This Post Helpful? 1
  • +
  • -

#7 Epic800  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-August 11

Re: Sorting Students?

Posted 27 November 2011 - 04:35 PM

View Postblackcompe, on 27 November 2011 - 04:28 PM, said:

Quote

How would I go about sorting both the arrays?


You've completely neglected to swap the names in n when you swap integers in a.


How would I swap the names?
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Sorting Students?

Posted 27 November 2011 - 04:35 PM

If you HAVE to use arrays, sorting can be accomplished as the user enters the names and the scores. First, get the name of the student and store temporarily as "name", then get the score and store temporarily as "score". Determine the index where the score belongs in the scores[] array, store score there, and then store name at the same index in the names[] array. You will have to insert items into each array, moving existing items out of the way as necessary.
Was This Post Helpful? 1
  • +
  • -

#9 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Sorting Students?

Posted 27 November 2011 - 04:46 PM

View PostEpic800, on 27 November 2011 - 04:35 PM, said:

View Postblackcompe, on 27 November 2011 - 04:28 PM, said:

Quote

How would I go about sorting both the arrays?


You've completely neglected to swap the names in n when you swap integers in a.


How would I swap the names?


Exactly how you swapped the integers. In the bubble sort, after you swap the integers, swap the names. Try it and see what happens. Post your updated code if you're still having problems.

Think about it. If your using parallel arrays, when you swap two integers in the integer array, then you want to swap the corresponding names (which reside at the same indices).

This post has been edited by blackcompe: 27 November 2011 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

#10 Epic800  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-August 11

Re: Sorting Students?

Posted 27 November 2011 - 05:33 PM

View Postblackcompe, on 27 November 2011 - 04:46 PM, said:

View PostEpic800, on 27 November 2011 - 04:35 PM, said:

View Postblackcompe, on 27 November 2011 - 04:28 PM, said:

Quote

How would I go about sorting both the arrays?


You've completely neglected to swap the names in n when you swap integers in a.


How would I swap the names?


Exactly how you swapped the integers. In the bubble sort, after you swap the integers, swap the names. Try it and see what happens. Post your updated code if you're still having problems.

Think about it. If your using parallel arrays, when you swap two integers in the integer array, then you want to swap the corresponding names (which reside at the same indices).



This is what I did but I get an error at line 23.

import java.util.Scanner;

public class Exercise06_19 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        
        
        System.out.print("Enter the number of students: ");
        int numberOfStudents = input.nextInt();

        int[] scores = new int[numberOfStudents];

        String[] names = new String[numberOfStudents];

        for (int i = 0; i < numberOfStudents; i++) {
            System.out.println("Enter the student's name: ");
            names[i] = input.next();
            System.out.println("Enter the student's score: ");
            scores[i] = input.nextInt();
        
        bubbleSort(names);
        
        System.out.println(names[i] + " has a score of " + scores[i]);


    }
    }

    public static void bubbleSort(int[] scores, String[] names) {
        boolean changed = true;
        do {
            changed = false;
            for (int j = 0; j < scores.length - 1; j++) {
                if (scores[j] > scores[j + 1]) {

                    int temp = scores[j];
                    scores[j] = scores[j + 1];
                    scores[j + 1] = temp;
                    changed = true;
                }
                
                    String temp = names[j];
                    names[j] = names[j + 1];
                    names[j + 1] = temp;
                    changed= true;
                    
                }
            
        } while (changed);
    }
}


This post has been edited by Epic800: 27 November 2011 - 05:33 PM

Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: Sorting Students?

Posted 27 November 2011 - 05:35 PM

Look at your bubbleSort() method: public static void bubbleSort(int[] scores, String[] names) {. You only pass one of the arrays to the bubbleSort() method when you call it. You have to pass both in the order specified. Also, in the future, remember to post your exact errors.
Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Sorting Students?

Posted 27 November 2011 - 05:37 PM

The method passes two parameters but you only passed one of them, look at the method definition:
public static void bubbleSort(int[] scores, String[] names) {
and you called it bubbleSort(names);
Was This Post Helpful? 0
  • +
  • -

#13 Epic800  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-August 11

Re: Sorting Students?

Posted 27 November 2011 - 05:41 PM

View Postsmohd, on 27 November 2011 - 05:37 PM, said:

The method passes two parameters but you only passed one of them, look at the method definition:
public static void bubbleSort(int[] scores, String[] names) {
and you called it bubbleSort(names);


Thanks, I see. I am now having problems getting the program to execute after I enter the grade.

This post has been edited by Epic800: 27 November 2011 - 05:43 PM

Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Sorting Students?

Posted 27 November 2011 - 05:56 PM

To swap both you will have to swap both if a condition to swap occurs

    public static void bubbleSort(int[] scores, String[] names) {
        boolean changed = true;
        do {
            changed = false;
            for (int j = 0; j < scores.length - 1; j++) {
                if (scores[j] > scores[j + 1]) {

                    int temp = scores[j];
                    scores[j] = scores[j + 1];
                    scores[j + 1] = temp;
                    changed = true;
                
                    String temp = names[j];
                    names[j] = names[j + 1];
                    names[j + 1] = temp;
                    changed= true;
                    
                }
            
        } while (changed);
    }
}


But this is not the way to go
Look at macosxnerd101 tutorial
http://www.dreaminco...arallel-arrays/
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1