Results 1 to 8 of 8
 04182008, 06:40 AM #1Member
 Join Date
 Apr 2008
 Posts
 4
 Rep Power
 0
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.length1; 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);
}
}
 04182008, 06:58 AM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
How did you handle the user inputs. I mean, on the text field on put one two how did you separate them?
 04182008, 08:43 PM #3Member
 Join Date
 Apr 2008
 Posts
 4
 Rep Power
 0
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?
 04212008, 05:00 AM #4
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
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.
 04232008, 06:05 AM #5Member
 Join Date
 Apr 2008
 Posts
 4
 Rep Power
 0
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;
}
}
 04232008, 06:11 AM #6
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Here I can't see use the names string array here in this code. You have passed it as a parameter, but not use.
 04262008, 12:15 AM #7Member
 Join Date
 Apr 2008
 Posts
 4
 Rep Power
 0
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;
}
}
 04282008, 05:28 AM #8
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
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};
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
Last edited by Eranga; 04282008 at 05:32 AM.
Similar Threads

how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04222008, 04:21 AM 
return Set .toArray(); method as an array of integers
By maxim in forum New To JavaReplies: 2Last Post: 04162008, 01:35 PM 
Sorting an array of Strings
By Java Tip in forum java.langReplies: 0Last Post: 04152008, 08:39 PM 
Using reflection to create, fill, and display an array
By Java Tip in forum java.langReplies: 0Last Post: 04142008, 09:43 PM 
Sorting, Searching, and Inserting into a sorted array
By Java Tip in forum java.langReplies: 0Last Post: 04142008, 09:39 PM
Bookmarks