Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: If-Else Statement help

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If-Else Statement help

    Hello,

    I just recently started a Java programming class and the current assignment is to create a program that asks the user if they are happy. Depending on the response either a smiley face or sad face appears in a window.

    I have been struggling to figure out how to set up an if-else statement, and am also unsure of when to use:
    public static void main(String[] args)

    I know this is probably very basic level stuff but I've tried messing around with the code for over 2 hours now. This is how far I've gotten..

    import java.awt.Graphics;
    import java.util.Scanner;
     
    import javax.swing.JApplet;
     
    /**
     *This is a program that displays different faces
     *based off of different responses.  
     *2/5/2010
     */
    public class Program3
    extends JApplet {
        public static final int FACE_DIAMETER = 200;
        public static final int X_FACE = 100;
        public static final int Y_FACE = 50;
        public static final int EYE_WIDTH = 10;
        public static final int EYE_HEIGHT = 20;
        public static final int X_RIGHT_EYE = 155;
        public static final int Y_RIGHT_EYE = 100;
        public static final int X_LEFT_EYE = 200;
        public static final int Y_LEFT_EYE = Y_RIGHT_EYE;
        public static final int MOUTH_WIDTH = 100;
        public static final int MOUTH_HEIGHT = 200;
        public static final int X_MOUTH = 150;
        public static final int Y_MOUTH = 160;
        public static final int MOUTH_START_ANGLE = 180;
        public static final int MOUTH_EXTENT_ANGLE = 180;
     
        public void init()
        {
            setSize(500, 500);
        }
     
     
        public void happyFace (Graphics canvas)
        {
            //Draw face outline
            canvas.drawOval (X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
     
            //Draw eyes
            canvas.fillOval (X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);
            canvas.fillOval (X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);
     
            //Draw mouth
            canvas.drawArc (X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT,
                    MOUTH_START_ANGLE, MOUTH_EXTENT_ANGLE);
        }
     
        public void sadFace (Graphics canvas)
        {
        }
        	private static final long serialVersionUID = 1L;
        	public static final int FACE_DIAMETER2 = 200;
        	public static final int X_FACE2 = 100;
        	public static final int Y_FACE2 = 50;
     
        	public static final int EYE_WIDTH2 = 10;
        	public static final int EYE_HEIGHT2 = 20;
        	public static final int X_RIGHT_EYE2 = 155;
        	public static final int Y_RIGHT_EYE2 = 100;
        	public static final int X_LEFT_EYE2 = 200;
        	public static final int Y_LEFT_EYE2 = Y_RIGHT_EYE;
     
        	public static final int MOUTH_WIDTH2 = 100;
        	public static final int MOUTH_HEIGHT2 = 200;
        	public static final int X_MOUTH2 = 150;
        	public static final int Y_MOUTH2 = 150;
        	public static final int MOUTH_START_ANGLE2 = 150;
        	public static final int MOUTH_EXTENT_ANGLE2 = -120;
     
        	public void paint(Graphics canvas)
        	{
        		//Draw face outline:
        		canvas.drawOval(X_FACE2, Y_FACE2, FACE_DIAMETER2, FACE_DIAMETER2);
        		//Draw eyes:
        		canvas.fillOval(X_RIGHT_EYE2, Y_RIGHT_EYE2, EYE_WIDTH2, EYE_HEIGHT2);
        		canvas.fillOval(X_LEFT_EYE2, Y_LEFT_EYE2, EYE_WIDTH2, EYE_HEIGHT2);
        		//Draw mouth:
        		canvas.drawArc(X_MOUTH2, Y_MOUTH2, MOUTH_WIDTH2, MOUTH_HEIGHT2,
        				MOUTH_START_ANGLE2, MOUTH_EXTENT_ANGLE2);
     
        	happyFace (canvas);
        	sadFace (canvas);
     
     
        //The following code deals with if-else and asking the user if they are happy
     
        	System.out.println("Are you happy?");
     
        	if (answer == 'y') || (answer == 'yes')
     
     
        	}
     
        	{
    }
     
    }

    Thanks for any advice!
    Last edited by Json; February 9th, 2010 at 07:06 AM. Reason: Please use code tags


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If-Else Statement help

    Hi,

    i will try to help you out a bit

    The method "public static void main(String[] args)" is basically only needed as entry point for standard java programs, if your applet is run from a browser you wont need that function.

    And about the if,i would better write it in a way like

    if ((answer.equalsIgnoreCase("y") || (answer.equalsIgnoreCase("yes")) {
    ....

    this ensures String comparison is used, and if the user types Y or YES it will be also valid.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: If-Else Statement help

    And to use best practises with this you want to turn it around as well

    if (("y".equalsIgnoreCase(answer) || ("yes".equalsIgnoreCase(answer)) {

    // Json

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If-Else Statement help

    I tried your suggestions but it says answer cannot be resolved--what does that mean? Also, I'm running this program from eclipse.

  5. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If-Else Statement help

    Okay everything is working as far as I can tell, BUT...when the program loads it won't allow me to add input using the keyboard. Is it the braces? Or is there some coding wrong?

    import java.awt.Graphics;
    import java.util.Scanner;
     
    import javax.swing.JApplet;
     
    /**
     *This is a program that displays different faces
     *based off of different responses.  
     *2/5/2010
     */
    public class Program3 extends JApplet
    {
    public static void main(String[] args)
    {System.out.println("Are you happy?");
     
     
    }	public static final int FACE_DIAMETER = 200;
        public static final int X_FACE = 100;
        public static final int Y_FACE = 50;
        public static final int EYE_WIDTH = 10;
        public static final int EYE_HEIGHT = 20;
        public static final int X_RIGHT_EYE = 155;
        public static final int Y_RIGHT_EYE = 100;
        public static final int X_LEFT_EYE = 200;
        public static final int Y_LEFT_EYE = Y_RIGHT_EYE;
        public static final int MOUTH_WIDTH = 100;
        public static final int MOUTH_HEIGHT = 200;
        public static final int X_MOUTH = 150;
        public static final int Y_MOUTH = 160;
        public static final int MOUTH_START_ANGLE = 180;
        public static final int MOUTH_EXTENT_ANGLE = 180;
     
     
     
        public void happyFace (Graphics canvas)
        {happyFace (canvas);
            //Draw face outline
            canvas.drawOval (X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
     
            //Draw eyes
            canvas.fillOval (X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);
            canvas.fillOval (X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);
     
            //Draw mouth
            canvas.drawArc (X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT,
                    MOUTH_START_ANGLE, MOUTH_EXTENT_ANGLE);
        }
     
        public void sadFace (Graphics canvas)
        {sadFace (canvas);}
        	private static final long serialVersionUID = 1L;
        	public static final int FACE_DIAMETER2 = 200;
        	public static final int X_FACE2 = 100;
        	public static final int Y_FACE2 = 50;
     
        	public static final int EYE_WIDTH2 = 10;
        	public static final int EYE_HEIGHT2 = 20;
        	public static final int X_RIGHT_EYE2 = 155;
        	public static final int Y_RIGHT_EYE2 = 100;
        	public static final int X_LEFT_EYE2 = 200;
        	public static final int Y_LEFT_EYE2 = Y_RIGHT_EYE;
     
        	public static final int MOUTH_WIDTH2 = 100;
        	public static final int MOUTH_HEIGHT2 = 200;
        	public static final int X_MOUTH2 = 150;
        	public static final int Y_MOUTH2 = 150;
        	public static final int MOUTH_START_ANGLE2 = 150;
        	public static final int MOUTH_EXTENT_ANGLE2 = -120;
     
        	@SuppressWarnings({ "unused", "null" })
    		public void paint(Graphics canvas)
        	{
        		//Draw face outline:
        		canvas.drawOval(X_FACE2, Y_FACE2, FACE_DIAMETER2, FACE_DIAMETER2);
        		//Draw eyes:
        		canvas.fillOval(X_RIGHT_EYE2, Y_RIGHT_EYE2, EYE_WIDTH2, EYE_HEIGHT2);
        		canvas.fillOval(X_LEFT_EYE2, Y_LEFT_EYE2, EYE_WIDTH2, EYE_HEIGHT2);
        		//Draw mouth:
        		canvas.drawArc(X_MOUTH2, Y_MOUTH2, MOUTH_WIDTH2, MOUTH_HEIGHT2,
        				MOUTH_START_ANGLE2, MOUTH_EXTENT_ANGLE2);
     
        		{ 
        Object answer;
    	{Scanner keyboard = new Scanner(System.in);
        answer = keyboard.nextLine();}
     
     
     
     
     
    				if ((answer.equals("y") || (answer.equals("yes")))) Statement:   {
        				happyFace (canvas);
        			}
        			else sadFace (canvas);}
     
     
        			}
     
     
     
     
     
        	{
    }
        		}

  6. #6
    Junior Member
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If-Else Statement help

    Well i guess it is cause you can't use "System.in" for applets

    I have found this link explaining how to get key input in applets:
    Applet Tutorial: Keyboard Input

    I hope you can use it for JApplet too

Similar Threads

  1. Having trouble with an if statement. please help!!
    By humdinger in forum Loops & Control Statements
    Replies: 11
    Last Post: January 21st, 2010, 01:49 PM
  2. If Statement Java Need help
    By Keno888 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 25th, 2009, 01:53 AM
  3. another unreachable statement..
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2009, 12:15 PM
  4. unreachable statement?
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: September 11th, 2009, 10:06 AM
  5. If statement
    By Dave in forum Loops & Control Statements
    Replies: 2
    Last Post: August 27th, 2009, 10:11 AM