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

1. Member
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:

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

}

}

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

3. Member
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?

4. 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. Member
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++)
}
}

6. 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. Member
Join Date
Apr 2008
Posts
4
Rep Power
0
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;
}
}

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

#### Posting Permissions

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