Re: Printing rows of stars...more robust and revisited
Now, the return statement after the "System.exit(1)" will obviously
never be performed, but the compiler isn't that smart, and now you can
see all the final int values right from the start.
As for the elimination of the first for loop, there should be a lot
less overhead from method calling and object creation this way, since
you're now only making a single string once, and only calling a single
method to do it.
public class ShowMe
{
public static void main(String[] args)
{
final int first = 1, last, total, medium;
try
{
last = Integer.parseInt(args[0]);
}
catch (NumberFormatException e)
{
System.out.println("Sorry, I can't handle that number");
System.exit(1);
return;
}
total = first + last;
medium = total / 2;
char[] starHolder = new char[total];
java.util.Arrays.fill(starHolder, '*');
String allStars = new String(starHolder);
for (int rowCount = 1; rowCount < total; rowCount++)
{
if (rowCount <= medium)
{
// print more as you go up
System.out.println( allStars.substring(0,rowCount) );
}
else
{
// print less once you're above the medium.
System.out.println( allStars.substring(0,total - rowCount) );
}
}
}
}