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

• 04-18-2008, 05:40 AM
luvjoynt
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:

2) Then sorted by Names

Example:

Input:
"Ram 95 Ace 42 Joe 80"

Output:

Ram 95
Joe 80
Ace 42

Name Order:
Ace 42
Joe 80
Ram 95

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;
}

for (int i = 0; grade.hasMoreTokens(); i++){

}
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);

sortnumSelection(y);
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);

}

}
• 04-18-2008, 05:58 AM
Eranga
How did you handle the user inputs. I mean, on the text field on put one two how did you separate them?
• 04-18-2008, 07:43 PM
luvjoynt
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?
• 04-21-2008, 04:00 AM
Eranga
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.
• 04-23-2008, 05:05 AM
luvjoynt
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++)
}
}
• 04-23-2008, 05:11 AM
Eranga
Here I can't see use the names string array here in this code. You have passed it as a parameter, but not use.
• 04-25-2008, 11:15 PM
luvjoynt
Eranga:

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

for (int i = 0; i < grades.length - 1; i++){
int largest = i;
for(int j = largest + 1; j < grades.length; j++)

String tmp = names [largest];
names [largest] = names [i];
names [i] = tmp;
}
}
• 04-28-2008, 04:28 AM
Eranga
Ok, did your code search up to the final data. I mean if my two array and grades arrays as follows,

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

Code:

```Captain  65 JavaTip  56 Don  54```
So, where is Root?