Welcome to Dream.In.Code
Become a Java Expert!

Join 149,485 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,344 people online right now. Registration is fast and FREE... Join Now!




Newbie in Java

 
Reply to this topicStart new topic

Newbie in Java, Java modifying the to form the square out of whatever character is con

beachlounger
16 May, 2007 - 06:02 PM
Post #1

New D.I.C Head
*

Joined: 30 Jan, 2007
Posts: 1


My Contributions
How do I get started on this:

I want the inputted characters to print the length and width of the square. Thanks

import java.awt.*;
import javax.swing.*;

public class Square extends JApplet {
int inputNumber;

// obtain value from user
public void init()
{
String inputString = JOptionPane.showInputDialog(
"Enter square size:" );

inputNumber = Integer.parseInt( inputString );

} // end method init
{
String inputString = JOptionPane.showInputDialog(
"Enter Character:" );

inputNumber =Integer.parseInt( inputString );
}


// draw square of
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

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Newbie In Java
16 May, 2007 - 08:53 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
what?
you're going to have to be more specific about your question/problem.
User is offlineProfile CardPM
+Quote Post

simonwatz
RE: Newbie In Java
16 May, 2007 - 10:08 PM
Post #3

New D.I.C Head
*

Joined: 10 Jul, 2006
Posts: 5


My Contributions
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 ph34r.gif

This post has been edited by simonwatz: 16 May, 2007 - 10:19 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 04:49PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month