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 8 of 8

Thread: How can I sort numbers from highest to lowest and keep names with them? Arrays

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How can I sort numbers from highest to lowest and keep names with them? Arrays

    I am making a program that asks for how many students and based on that answer a number of boxes come up asking for their name and their score. After it is supposed to sort them from best score to lowest score and keep the name with them. I honestly have no idea what I am doing and need some serious help.

    For the purpose of making this code I just hard coded scores and names.

    String numOfStudents = JOptionPane.showInputDialog("Enter number of students: ");
         int numOfStudents1 = Integer.parseInt(numOfStudents);  
     
         int[] scores = new int[numOfStudents1];
      String [] names = new String[numOfStudents1];
     
     scores[0]= 67;
      scores[1]= 98;
      scores[2]= 56;
      scores[3]= 78;
      scores[4]= 100; 
     
      names[0] = "Student 1";
      names[1] = "Student 2";
      names[2] = "Student 3";
      names[3] = "Student 4";
      names[4] = "Student 5";
     
      sortArray(scores);
     
            System.out.println(names[0] + scores[0]);
             System.out.println(names[1] + scores[1]);
              System.out.println(names[2] + scores[2]);
               System.out.println(names[3] + scores[3]);
                System.out.println(names[4] + scores[4]);
        }
     
     
     
        public static void sortArray(int[] scores) {
     
       for (int i = 0; i < scores.length; i++) {
            int lowLoc = i;
            int low = scores[i];
     
     
          for (int x = i; x < scores.length; x++) {
            if(low>scores[x]){
            low = scores[x];
            lowLoc = x;
            }//end of if
        }//end of inner for
           }//end of for    
     
     
    }//end of sortArray
     
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How can I sort numbers from highest to lowest and keep names with them? Arrays

    Java is object-oriented, so my advice would be to take advantage of that feature and create a class which contains the student name and score. You can then create an object/instance of said class containing the appropriate data. You can then sort an array of these and all the information is self-contained within the objects.
    See Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How can I sort numbers from highest to lowest and keep names with them? Arrays

    This all has to be in one class. Thanks for the response though!

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How can I sort numbers from highest to lowest and keep names with them? Arrays

    You have an array of scores and an array of names.

    You will use the scores to sort them. So what ever happens to the score array should happen to the names array at the same time.

    How will you sort the scores to begin with? Get that part working first and worry about letting the names tag along for the ride later on.

    Edit: For the record if this was designed to teach arrays, I can think of better demonstrations. This assignment tends to teach bad habits in terms of problem solving. Copeg's suggestion is the real answer to this assignment.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How can I sort numbers from highest to lowest and keep names with them? Arrays

    Like i said. I cannot use separate classes, they must all be in one. My question is how can I go about sorting them... I don't have a clue how to sort the scores.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How can I sort numbers from highest to lowest and keep names with them? Arrays

    90 43 98 58 69 20 75 73 40

    Can you sort these numbers?

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How can I sort numbers from highest to lowest and keep names with them? Arrays

    I dont think so... because my sortArray method doesnt work how I thought it would...

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How can I sort numbers from highest to lowest and keep names with them? Arrays

    Quote Originally Posted by ColeTrain View Post
    Like i said. I cannot use separate classes, they must all be in one. My question is how can I go about sorting them... I don't have a clue how to sort the scores.
    Quote Originally Posted by ColeTrain View Post
    I dont think so... because my sortArray method doesnt work how I thought it would...
    I meant can you sort the numbers without the program. Just sort them. Think about how you would go about finding the largest or smallest number from all of them. Write down the steps necessary to sort numbers first, and then write code that performs those steps. You know how to sort numbers, and you know how to write code. The problem is you are trying to write code to solve a problem instead of writing code to perform steps that were designed to solve the problem.

Similar Threads

  1. Replies: 2
    Last Post: November 11th, 2012, 10:44 PM
  2. [SOLVED] flightpaths, finding lowest cost -- lowest amount of crossovers
    By CjStaal in forum Algorithms & Recursion
    Replies: 4
    Last Post: May 8th, 2012, 12:47 AM
  3. Help with using mergesort to sort a list of names alphabetically?
    By BearBearBear in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 27th, 2012, 09:10 AM
  4. Bubble Sort with random numbers
    By jake6047 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 20th, 2011, 09:39 AM
  5. Help with Sort arrays
    By drk in forum Collections and Generics
    Replies: 5
    Last Post: September 6th, 2009, 02:48 AM