public class Asterisk
{
public static void main(String[] args)
{
String output="";
int count=1, input;
System.out.println("Input the size of the triangle from 1 to 50:");
input = 5;
for(count=1;count <= input;count++)
{
output += "*";
System.out.println(output);
}
input -= 1;
for(count =input;count <= input;count--)
{
output = output.substring(0,count);
System.out.println(output);
}
}
}
My code compliles correctly, and runs correctly too. However at the bottom of the output it prints an error saying:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: -1
at java.lang.String.substring(String.java:1937) at Asterisk.main(Asterisk.java:18)
Can anyone explain this strange behavior? Thanks!