How do I fix this program (2 errors)?
This program needs to accept an input (Odd number, between 3-5) and then it will use that number to output a Diamond. The user input will be the center of the diamond. For example, if the user inputs 9 the diamond will look like this
(Note: the formatting rules of this forum delete the spaces so I can't format this as a proper diamond but you get the idea)
*
***
*****
*******
*********
*******
*****
***
*
I get 2 errors:
The local variable diamondString may not have been initialized
Unhandled exception type IOException
Code:
public class Diamond {
public static void main(String[] args)
{
CollectInput();
}
public static void CollectInput()
{
int num;
System.out.println("Please input an odd number between 3 and 15.");
num = System.in.read();
}
public void CreateRows(int num)
{
int row;
int spaces;
int stars;
String diamondString;
for (row = 1; row <=num/2+1;row ++)
{
for (spaces = num; spaces >row; spaces--)
{
diamondString += " ";
}
for(stars=1; stars <=(2 *row)-1;stars++)
{
diamondString +="*";
}
diamondString +="\n";
}
System.out.println(diamondString);
for (row = 1; row <=num/2+1;row ++)
{
for (spaces = num; spaces >row; spaces--)
{
diamondString += " ";
}
for(stars=1; stars <=(row/2)-1;stars++)
{
diamondString +="*";
}
diamondString +="\n";
}
System.out.println(diamondString);
}
}
I don't know if my logic is right for the top half of the diamond but i'm not sure about the bottom. I'm hoping being able to see the output will allow me to guess/check the logic until it's correct.
Any help would be appreciated