Results 1 to 8 of 8
  1. #1
    luvjoynt is offline Member
    Join Date
    Apr 2008
    Posts
    4
    Rep Power
    0

    Question Help with Code! Display Array of Strings and Integers - Sorted

    I'd like to preface my code with the fact that I am a student and have been working on a solution to his problem for the past two weeks and I'm missing one last thing. After I input a list of strings and integers into a dialog box, it should return them:

    1) Sorted by Grades
    2) Then sorted by Names

    Example:

    Input:
    "Ram 95 Ace 42 Joe 80"

    Output:

    Grade Order:
    Ram 95
    Joe 80
    Ace 42

    Name Order:
    Ace 42
    Joe 80
    Ram 95

    Please Help! Here's my code so far:

    import java.util.StringTokenizer;
    import javax.swing.JOptionPane;


    public class labPractice7 {

    public static float getAverage (int g[]){
    float sum = 0.0f;
    for (int i = 0; i < g.length; i++)
    sum += g[i];
    return sum / g.length;
    }

    public static int getHighest(int g[]){
    int high = Integer.MIN_VALUE;
    for (int i = 0; i < g.length;i++)
    if (g[i] > high) high = g[i];
    return high;
    }

    public static int getLowest(int g[]){
    int low = Integer.MAX_VALUE;
    for (int i = 0; i < g.length; i++)
    if (g[i]<low) low = g[i];
    return low;
    }

    public static String[] getNames (String input){
    StringTokenizer token = new StringTokenizer(input);
    String []r = new String[token.countTokens()/2];
    for (int i = 0; token.hasMoreTokens(); i++){

    r[i] = token.nextToken();
    token.nextToken();
    }
    return r;
    }

    public static int[] getGrades(String input){
    StringTokenizer grade = new StringTokenizer(input);
    int []r = new int[grade.countTokens()/2];
    for (int i = 0; grade.hasMoreTokens(); i++){

    grade.nextToken();
    r[i] = Integer.parseInt(grade.nextToken());

    }
    return r;
    }
    public static void sortcharSelection(String n[]){
    for (int i = 0; i < n.length-1; i++){
    int smallest = i;
    for(int j=i+1; j < n.length; j++)
    if (n[j].compareTo(n[smallest])<0)
    smallest = j;
    String tmp = n[i];
    n[i] = n[smallest];
    n[smallest] = tmp;
    }
    }

    public static void sortnumSelection(int list[]){

    for (int i = 0; i < list.length - 1; i++){
    int largest = i;
    for(int j = largest + 1; j < list.length; j++)
    if (list[j] > list[largest]) largest = j;
    int temp = list[largest];
    list[largest] = list[i];
    list[i] = temp;
    }
    }

    public static void main(String[] args) {

    String input = JOptionPane.showInputDialog("Enter one or more names and grades");
    String data[] = input.split("\\s");
    String []x = getNames(input);
    int []y = getGrades(input);

    sortnumSelection(y);
    String result= "Grade Order:\n";
    for (int i = 0; i < y.length; i++) result += x[i]+ " " +y[i]+ "\n";

    sortcharSelection(x);
    result+= "\nName Order:\n";
    for (int i = 0; i < x.length; i++) result += x[i]+ " "+ y[i]+"\n";

    result += String.format("\nAverage%7.2f" + "\nHighest:%3d" + "\nLowest:%4d",getAverage(y),getHighest(y),getLowe st(y));
    IO.showMsg(result, "Results");
    System.exit(0);

    }

    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    How did you handle the user inputs. I mean, on the text field on put one two how did you separate them?

  3. #3
    luvjoynt is offline Member
    Join Date
    Apr 2008
    Posts
    4
    Rep Power
    0

    Default

    Eranga:

    One the input box appears, I type:

    Ram 95 Ace 42 Joe 80

    I separate the names and grades with spaces. For instance Ram and 95 should be in an array... I think?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, after separating name and number from the input string, did you store them in an array? Have to use two array foe this, one for name and one for numbers. Them sort them.

    There are lots of way to sorting in Java?

    Did you get what I say.

  5. #5
    luvjoynt is offline Member
    Join Date
    Apr 2008
    Posts
    4
    Rep Power
    0

    Default

    I am having a problem with the following method. When the method sorts, the name doesn't follow the grade that gets sorted.


    public static void sortnumSelection(String names[], int grades[]){
    for (int i = 0; i < grades.length - 1; i++){
    int largest = i;
    for(int j = largest + 1; j < grades.length; j++)
    if (grades[j] > grades[largest]) largest = j;
    int temp = grades[largest];
    grades[largest] = grades[i];
    grades[i] = temp;
    }
    }

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Here I can't see use the names string array here in this code. You have passed it as a parameter, but not use.

  7. #7
    luvjoynt is offline Member
    Join Date
    Apr 2008
    Posts
    4
    Rep Power
    0

    Default

    Eranga:

    I finally got it fixed. The following method sorts and array Names and Grades.

    public static void sortGrades(String names[], int grades[]){
    for (int i = 0; i < grades.length - 1; i++){
    int largest = i;
    for(int j = largest + 1; j < grades.length; j++)
    if (grades[j] > grades[largest]) largest = j;
    int temp = grades[largest];
    grades[largest] = grades[i];
    grades[i] = temp;

    String tmp = names [largest];
    names [largest] = names [i];
    names [i] = tmp;
    }
    }

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, did your code search up to the final data. I mean if my two array and grades arrays as follows,

    Java Code:
            String[] name = {"JavaTip", "Don", "Captain", "Root"};
            int[] grades = {56, 54, 65, 34};
    What is the out put? You don't make the last on sorted, right?

    Actually you miss a simple point, referring the array to search. Can you find it? ;) If not let me know. :)

    For me, output is

    Java Code:
    Captain  65
    JavaTip  56
    Don  54
    So, where is Root?

    Last edited by Eranga; 04-28-2008 at 04:32 AM.

Similar Threads

  1. Replies: 8
    Last Post: 04-22-2008, 03:21 AM
  2. Replies: 2
    Last Post: 04-16-2008, 12:35 PM
  3. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:39 PM
  4. Replies: 0
    Last Post: 04-14-2008, 08:43 PM
  5. Replies: 0
    Last Post: 04-14-2008, 08:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •