0

I am trying to create a program that outputs a histogram based on

  • User input regarding how many lines the histogram should be, and
  • the actual numbers (which are also user input)

The program works fine, EXCEPT that I get the message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at test.Test.main(Test.java:32) /Users/[myname]/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 7 seconds)

when the program has finished. What is the problem?

 public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String star = "*";
        int index = 1;

        System.out.println("How many lines?");
        int num = input.nextInt();

        System.out.println("Your histogram will be "+ num +" lines");
        int[] histogram = new int[num];

        for (int i = 0; i < histogram.length; i++) {

            System.out.println("Please write number " + index++);

            histogram[i] = input.nextInt();

        }

        System.out.println("Here is your histogram: ");           

        for (int i = 0; 1 < histogram.length; i++) {
            for (int j = 0; j < histogram[i]; j++)

            System.out.print(star);
            System.out.println();

        }

    System.exit(0);

    }
}
1
  • 2
    for (int i = 0; 1 < histogram.length; i++) {, did you mean for (int i = 0; i < histogram.length; i++) { ? Commented Mar 15, 2017 at 13:59

2 Answers 2

2

Replace:

for (int i = 0; 1 < histogram.length; i++) {

by:

for (int i = 0; i < histogram.length; i++) {

Otherwise, it will execute the correct number of times (since i is still incremented, and the inner loop loops up till i) and crash when it reaches "OutofBounds", but since the loop is at the end of your program it looks like it executes correctly.

Sign up to request clarification or add additional context in comments.

Comments

0

The error occurs because you wrote 1 < histogram.length when you actually mean i < histogram.length.

If you don't know where I'm referring to, here it is:

    System.out.println("Here is your histogram: ");           

    for (int i = 0; 1 < histogram.length; i++) { // <-- this line right here
        for (int j = 0; j < histogram[i]; j++)

        System.out.print(star);
        System.out.println();

    }

1 < histogram.length causes the loop to never end. Therefore, after it printed all the histogram rows, i increments and it exceeds the size of the array. Since all the things that you wanted it to print have been printed at this point, it seems like you get the correct output.

Basically, a program will keep printing things until there is an exception or the end of the program is reached. The presence of an exception does not make the whole program output nothing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.