Hey beachlounger,
I picked at what you have already got use
WinMerge and compare my code to yours to see the difference. Or you can do it in
Notepad++Note:
When posting code you must use the code tags.
Like So.....
CODE
import java.awt.*;
import javax.swing.*;
public class square extends JApplet
{
public static int inputNumber;
public static char inputChar;
// obtain value from user
//where was your main method
public static void main(String[] args)
{
sqSize();
myChar();
}
public static void sqSize()
{
String inputString;
boolean invString = true;
do
{
inputString = JOptionPane.showInputDialog(null,
"Enter square size:" );
//before it parses the string into a int this for loop checks each individual alphanumeric character
//in the string to make sure it isn't a letter, if it is it prompts the user to enter the data again.
for(int i = 0; i < inputString.length();i++)
{
if(Character.isLetter(inputString.charAt(i)))
invString = true;
else
invString = false;
}
if(invString == true)
JOptionPane.showMessageDialog(null,"That was not a number." +
"\nPlease try again.");
}while(invString == true);
inputNumber = Integer.parseInt( inputString );
//test it worked
JOptionPane.showMessageDialog(null, "The size you entered was: "
+ inputNumber);
}
public static void myChar()
{
String inputAChar;
boolean invChar = true;
do
{
inputAChar = JOptionPane.showInputDialog(null,
"Enter Character:" );
//same story except checks if it is a character
for(int i = 0; i < inputAChar.length();i++)
{
if(!Character.isLetter(inputAChar.charAt(i)))
invChar = true;
else
{
inputChar = inputAChar.charAt(i);//assigns the valid character at string index i to inputChar
invChar = false;
}
}
if(invChar == true)
JOptionPane.showMessageDialog(null,"That was not a character." +
"\nPlease try again.");
}while(invChar == true);
//test it worked
JOptionPane.showMessageDialog(null, "The character you entered was: "
+ inputChar);
//i wouldn't recommend changing your entered character into a int.......
}
// draw square of
//where is the variable 'side' being initialized?? you are telling your for loop to multiple null by null
public void squareOfCharacters( Graphics g, int side )
{
int y = 50, x = 5;
for ( int count = 1; count <= side * side; count++ )
{
g.drawString( "*", x += 5, y );
if ( count % side == 0 )
{
y += 10;
x = 5;
}
} // end for loop
} // end method squareOfCharacters
// execute method squareOfCharacters
public void paint( Graphics g )
{
squareOfCharacters( g, inputNumber );
} // end method paint
} // end class Square
I hope this gets you on your way.
If you have any questions do not hesitate to ask.
Cheers
SW
This post has been edited by simonwatz: 16 May, 2007 - 10:19 PM