How to write a java program using arrays?

Blackcompe

New member
Your code throws an exception in getData(String). You didn't give the tokenizer the delimiter argument, which is the regular expression you are required to use.

Eh.... let me clean this up, brb.
 
I need to write a java program using net beans that will make a method that will ask the user to enter names and grades.

(input will be something like: joe 94 mary 89 johnny 78)

I need to be able to put the information that the user enters into an array. Then I need to check right then if the order goes from name to grades respectively. I need to use regular expressions like using "\s+". If every odd array is not a number then i need to print out a message to the console.

I also need a method that will be able to take in the names and using string tokenizer or sort selection, be able to alphabetize the names. This is what i have so far, the grades sorting works but i am stuck there because I am supposed to let the user enter as many names and numbers as they want...

import javax.swing.*;
import java.util.*;


public class Program7 {


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

public static int[] getData (String number){
StringTokenizer token = new StringTokenizer(number);
int [] a = new int[token.countTokens()];
for (int i = 0; token.hasMoreTokens(); i++)
a = Integer.parseInt(token.nextToken());
return a;
}

public static void sortSelection(int grades[]){
for (int i = 0; i < grades.length -1; i++){
int largest = i;
for (int c = largest + 1; c < grades.length; c++)
if (grades[c]> grades[largest]) largest = c;
int temp = grades[largest];
grades[largest]= grades;
grades= temp;

}
}

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

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

public static Scanner getInput (String prompt){
String s = JOptionPane.showInputDialog(prompt);
return new Scanner(s);
}

public static void showMsg(String s){
JOptionPane.showMessageDialog(null,s);
System.out.println(s);
}

public static void showMsg (String s, String title){
JOptionPane.showMessageDialog(null, s, title, JOptionPane.PLAIN_MESSAGE);
System.out.println (s);
}
public static String[] getNames(int n){
String str =JOptionPane.showInputDialog(
"Enter"+ n + "names");
return str.split("\s+");
}


public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Enter grades and names");
int number = 0;
int [] x = getData(input);

String output = "Grade order ";
for(int i = 0; i < x.length ; i++ ) output += " " +x;
output+="\nName Order: ";
sortSelection(x);
for(int i = 0; i < x.length ; i++ ) output += " " +x;
output += "\nThe average grade is: " + getAvg(x) + "\nThe highest grade is: " + getHighest(x) + "\nThe lowest grade is: " + getLowest(x);
showMsg(output, "Your Answer");



System.exit(0);
}

}
 
Back
Top