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

}

Recommended Answers

All 4 Replies

Hey there!

Thanks for upgrading to a DaniWeb Premium membership. Unfortunately I don't know Java (two courses in college 20 years ago is the extent of my Java knowledge). However, an array index out of bounds exception means that you have an array, and you're trying to retrieve a specific index of the array that doesn't exist. For example, if you have an array with 5 elements, you're trying to retrieve array[8] or something like that.

Usually the error message will specify the line that triggered the error. Can you let us know what line that is and then we can look into it.

commented: It was line 124 +0

Line 124 of the above code says: names[j + 1] = tmpname;

Please keep in mind that it looks like the first three lines of your code above were not properly indented so our forum system did not add line numbers to them.

I just want to clarify you did mean line 124 of the code you posted here.

Let's assume it is ... By the time we get to line 124, how many elements are in the array names? Is it possible for there to be fewer than j elements in the array, such that index j+1 doesn't exist?

Something tells me that line 124 in the error you got does not correspond to line 124 in the code you posted above.

Yes.
If the names array is the same size as the x array (as seems probable) then in that loop j goes from 0 to x.length-1, so the [j+1] index will go out bounds on the last pass of the loop.

ps Oracle's and Apache's Java coding standards prefer String[] names overString names[]. Although the latter is valid Java, the first version is more consistent with the overall syntax of <type> <name>, ie names is of type String[]

Hi Andrew,

Were you able to figure this out?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.