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);
}
}
for (int i = 0; 1 < histogram.length; i++) {, did you meanfor (int i = 0; i < histogram.length; i++) {?