Results 1 to 8 of 8

Thread: If Statements

  1. #1
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default If Statements

    I'm adding this text box and button to a Java Applet i'm making

    no matter what i enter into the textbox, the only thing it prints is "No match"

    any idea where i'm going wrong?

    Thanks
    Java Code:
    import java.applet.Applet;
    import java.awt.Button;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    
    	public class button extends Applet  implements ActionListener
    	{	
    	
    		//TEXT BOX AND BUTTON
    			 
    		     Button okButton;								   					
    		     TextField nameField;
    		     {
    		    	setLayout(null);
    		        okButton = new Button("Search");
    		        
    		        nameField = new TextField("",100);
    		     }
    
    		        {
    		          okButton.setBounds(140,20,160,25);
    		          nameField.setBounds(20,20,100,25);
    		        } 
    
    		        {
    		      add(okButton);
    		      add(nameField);
    		      okButton.addActionListener(this);
    		     }
    		        public void actionPerformed(ActionEvent e) {
    		        	
    if (nameField.equals("Resturant")) 
    System.out.println("All Resturants Have Air-Conditioning");
    
    else if (nameField.equals("Picnic Area")) 
    	System.out.println("All Picnin Areas are non-Smoking");
    
    else if (nameField.equals("Play Areas")) 
    	System.out.println("All Play Areas are for Under-12's");
    
    else if (nameField.equals("Lakes")) 
    	System.out.println("All Lakes are Non-Swimming");
    
    else if (nameField.equals("River")) 
    	System.out.println("River is deep, Keep small children away from edge");
    
    else 
    	System.out.println("No Match");
    Last edited by Eranga; 02-29-2012 at 05:57 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: If Statements

    Check this line:

    Java Code:
    nameField.equals("Picnic Area") {
    nameField is a TextField object and will never "equal" a String. Perhaps instead you would like to extract the text held by the nameField and test that against your Strings. The API for TextField will tell you which method to use (actually, it's a method of its parent class, TextComponent).

    Question -- is this for a school assignment or a personal project?
    Last edited by Fubarable; 02-29-2012 at 01:46 AM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: If Statements

    Java Code:
    if (nameField.equals("Resturant"))
    This line compares the text field nameField with the string "Resturant" and, of course, they are never equal because strings and text fields are different things. You should compare the string "Resturant" with the string returned by the text field:

    Java Code:
    if(nameField.getText().equals("Resturant")) {
        System.out.println("whatever");
    }
    Note also the use of braces - these are a good idea even with one line blocks of code.

    Also when you post code, use the "code" tags. You put [code] at the start of the code and [/code] at the end. That way the code will be nicely formatted when it appears here.

  4. #4
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: If Statements

    this is for a website a friend is building

    is there any way that you can get it to display to the applet rather than the console?

  5. #5
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: If Statements

    also when i put it into the main applet program it tells me that "void is an invalid type for the variable
    actionPerformed" despite it working in its own project

    Java Code:
    public void actionPerformed(ActionEvent e)
    any suggestions on why that might be?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: If Statements

    Quote Originally Posted by WillHorne View Post
    this is for a website a friend is building
    then don't use Applet but move up to Swing with a JApplet. Or perhaps better still, use Java Web Start and a Swing application.

    is there any way that you can get it to display to the applet rather than the console?
    Go through the Swing tutorials as it will show you how to create full-fledged GUI's.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: If Statements

    Quote Originally Posted by WillHorne View Post
    also when i put it into the main applet program it tells me that "void is an invalid type for the variable
    actionPerformed" despite it working in its own project

    Java Code:
    public void actionPerformed(ActionEvent e)
    any suggestions on why that might be?
    Are you trying to nest one method into another? Because this can't be done in Java. How much Java do you know? You probably should go through the introductory tutorials before trying to tackle GUI coding because without the foundations you may be in for a world of frustration.

  8. #8
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: If Statements

    yea that just clicked

    i'd been up for about 36 hours at that point and things were a bit fuzzy

    Ive been going through the JLabel and still don't have much of an idea how they fit together

    could you give me a working example of how the code fits together

    Thanks

Similar Threads

  1. Help with IF Statements and GUI
    By university123 in forum New To Java
    Replies: 8
    Last Post: 10-14-2010, 05:38 AM
  2. if else if statements
    By Allspark in forum New To Java
    Replies: 9
    Last Post: 09-28-2010, 07:50 PM
  3. Help with if else statements
    By np2392 in forum New To Java
    Replies: 2
    Last Post: 09-24-2010, 02:25 AM
  4. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 08:02 PM
  5. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 05:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •