When i run my code i get this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: How do I fix this? this is my code so far:
import java.awt.Font;
import javax.swing.*;
public class Program7
public static void showOutput(String output) {
JTextArea jta = new JTextArea(output);
jta.setFont(new Font("Consolas", Font.PLAIN, 60));
JOptionPane.showMessageDialog(null, jta);
}
public static int getHighest(int x[]) {
int hi = x[0];
for (int i = 1; i < x.length; i++) {
if (x[i] > hi) {
hi = x[i];
}
}
return hi;
}
public static int getLowest(int x[]) {
int low = x[0];
for (int i = 1; i < x.length; i++) {
if (x[i] < low) {
low = x[i];
}
}
return low;
}
public static int[] getGrades(String input) {
String data[] = input.trim().split("\\s+");
int x[] = new int[data.length / 2];
for (int i = 1; i < data.length; i += 2) {
if (data[i].matches("\\d+")) {
x[i / 2] = Integer.parseInt(data[i]);
} else {
System.err.println("inpiy error");
}
}
return x;
}
public static int[] sortSelection(int x[]) {
for (int i = 0; i < x.length - 1; i++) {
int largest = i;
for (int j = largest + 1; j < x.length; j++) {
if (x[j] > x[largest]) {
largest = j;
}
}
int tmp = x[largest];
x[largest] = x[i];
x[i] = tmp;
}
return x;
}
public static int[] sortBubble(int x[]) {
for (int i = 0; i < x.length - 1; i++) {
for (int j = 0; j < x.length - 1; j++) {
if (x[j + 1] > x[j]) {
int tmp = x[j + 1];
x[j + 1] = x[j];
x[j] = tmp;
}
}
}
return x;
}
public static int[] getData(String input) {
String data[] = input.trim().split("\\s+");
int x[] = new int[data.length];
for (int i = 0; i < data.length; i++) {
try {
x[i] = Integer.parseInt(data[i]);
} catch (NumberFormatException nfe) {
System.err.println(nfe.getMessage());
x[i] = 0;
}
}
return x;
}
public static float getAverage(int x[]) {
float total = 0;
for (int i = 0; i < x.length; i++) {
total += x[i];
}
return total / x.length;
}
public static String putArray(String names[], int x[]) {
String result = "";
for (int i = 0; i < x.length; i++) {
result += names[i] + "\t" + x[i] + "\n";
System.out.println(x);
}
return result;
}
public static int[] sortToHigh(int x[]) {
for (int i = 0; i < x.length - 1; i++) {
int largest = i;
for (int j = largest + 1; j < x.length; j++) {
if (x[j] > x[largest]) {
largest = j;
}
}
int tmp = x[largest];
x[largest] = x[i];
x[i] = tmp;
}
return x;
}
public static int[] sortBubbleByGrade(String names[], int x[]) {
for (int i = 0; i < x.length; i++)
for (int j = 0; j < x.length; j++)
if (x[j + 1] > x[j]) {
int tmp = x[j + 1];
x[j + 1] = x[j];
x[j] = tmp;
String tmpname = names[j];
names[j] = names[j + 1];
names[j + 1] = tmpname;
}
return x;
}
public static int[] sortToLow(int x[]) {
for (int i = 0; i < x.length - 1; i++) {
int smallest = i;
for (int j = smallest + 1; j < x.length; j++) {
if (x[j] < x[smallest]) {
smallest = j;
}
}
int tmp = x[smallest];
x[smallest] = x[i];
x[i] = tmp;
}
return x;
}
public static String[] getNames(String input) {
String data[] = input.trim().split("\\s+");
String x[] = new String[data.length / 2];
for (int i = 0; i < data.length; i += 2) {
x[i / 2] = data[i];
}
return x;
}
public static int[] sortByName(String names[], int x[]) {
for (int i = 0; i < x.length - 1; i++) {
for (int j = 0; j < x.length - 1; j++) {
if (names[j + 1].compareTo(names[j]) < 0) {
int tmp = x[j + 1];
x[j + 1] = x[j];
x[j] = tmp;
String tmpname = names[j];
names[j] = names[j + 1];
names[j + 1] = tmpname;
}
}
}
return x;
}
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Enter 1 or more names and grades");
String names[] = getNames(input);
int grades[] = getGrades(input);
sortBubbleByGrade(names, grades);
String output = "Name \t Grades \n" + putArray(names, sortToHigh(grades));
sortByName(names, grades);
showOutput(output + "\nsorted\n" + putArray(names, grades) + String.format("\nAverage:%3.2\n"
+ "Highest:%d\n" + "Lowest:%d\n", getAverage(grades), getHighest(grades), getLowest(grades)));
}