Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Sorting Arrays

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Sorting Arrays

    Hello,

    I'm a beginner in Java and am having trouble putting this program exercise together / I think I got it and I may have just messed up somewhere along the lines.

    The program is to prompt the user for the number of students, the student names, and their scores. It should then print them out in decreasing order of their scores.

    Output should be:

    Enter number of students: 3
    Smith 70
    Jones 30
    Peterson 100

    The print out is
    Jones 30
    Smith 70
    Peterson 100

    This is what I have so far.


     
    import java.util.Scanner;
     
    public class Exercise06_19 {
     
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
     
            System.out.print("Enter number of Students: ");
            int numberOfStudents = input.nextInt();
     
            String[] name = new String[numberOfStudents];
            for (int i = 0; i < name.length; i++) {
                name[i] = input.next();//grab the string input values and put into the name array
            }
            int[] score = new int[numberOfStudents];
            for (int i = 0; i < score.length; i++) {
                score[i] = input.nextInt();//grab the integer input values and put them in the score array
            }
            decreasingOrder(name, score);
        }
     
        public static void decreasingOrder(String[] name, int[] score) {
     
            for (int i = 0; i < score.length - 1; i++) {
     
                int currentMin = score[i];
                int currentMinIndex = i;
                String currentMinName = name[i];
     
     
                for (int j = i + 1; j < score.length; j++) {
                    if (currentMin > score[j]) {
                        currentMin = score[j];
                        currentMinIndex = j;
                        currentMinName = name[j];//swap the string array values 
                    }
                }
     
     
                if (currentMinIndex != i) {
                    score[currentMinIndex] = score[i];
                    score[i] = currentMin;
                }
                System.out.println(name[i] + score[i]);
            }
     
        }
    }

    I'm trying to make it so when I input my first String value, it goes to String index 0, and when I input my first Integer value, it goes to Integer index 0, so they match and I can swap them by their indexes. Is this the right way to approach this program?

    The method is also where I get really confused, I'm not sure how to swap the name that goes with the integer inputed with it.
    This is what I have so far.

    Any kind of help/hints/tips/explanations is greatly appreciated

    Thanks!


  2. #2
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Sorting Arrays

    First of all your program doesnt ask for user input when inputing the students names and scores. It just expects the user to know what the program wants.

    Your first for loop in decreasingOrder() has score.length - 1 ... what is the minus 1 for?

    I suggest using if else statements to compare the scores of each student. If one score is higher than the other than do a quick exchange between them and then dont forget to swap the names as well. So compare score[0] to score[1], if index 0 is higher then it stays where it is, if not then it swaps with index 1. After that do score[1] compares to score[2], etc.

    to do a quick swap do the following idea:

    for(i = 0;i < score.length;i++)
    {
         if(score[i] < score[i + 1])
         {
              int swap = score[i];
              score[i] = score[i + 1];
              score[i + 1] = swap;
         }
    }

  3. The Following User Says Thank You to that_guy For This Useful Post:

    Bryan29 (November 27th, 2011)

  4. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Sorting Arrays

    Thanks a lot for the help and explanation The input is like that because it needs to look a certain way in order to go through as correct on his website where we turn these in.

    I switched it but I still get this error that I got before I switched it:

    Enter number of Students: 3
    Smith
    70
    Jones
    30
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:841)
    at java.util.Scanner.next(Scanner.java:1464)
    at java.util.Scanner.nextInt(Scanner.java:2108)
    at java.util.Scanner.nextInt(Scanner.java:2067)
    at Exercise06_19.main(Exercise06_19.java:17)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sorting Arrays

    InputMismatchException means that you entered the wrong type of data.
    The nextInt method on line 17 wants numeric data.

  6. #5
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Sorting Arrays

    Yes, what Norm said. Your program expects you to enter all 3 of the names first then all three of the scores.

    your program wants the following input:
    Smith
    Jones
    Peterson
    70
    30
    100

    To fix this either combine the for loops into one loop or have the computer prompt the user for the info so there is no confusion.

  7. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Sorting Arrays

    Advise: I will recommend you to use input.nextLine() instead of input.next(). If you don't know the difference, you must know it.

Similar Threads

  1. Sorting substring?
    By alpvii in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 23rd, 2011, 05:39 AM
  2. Digital Watch program and Sorting arrays
    By c.P.u1 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 5th, 2011, 05:21 PM
  3. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 08:34 AM
  4. Selection Sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 5
    Last Post: December 10th, 2009, 11:08 AM
  5. Sorting Algorithms
    By Dalisra in forum Java Programming Tutorials
    Replies: 1
    Last Post: November 10th, 2009, 09:24 PM