Results 1 to 8 of 8
Thread: If Statements
- 02-29-2012, 12:26 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 27
- Rep Power
- 0
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 04:57 AM. Reason: code tags added
-
Re: If Statements
Check this line:
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).Java Code:nameField.equals("Picnic Area") {
Question -- is this for a school assignment or a personal project?Last edited by Fubarable; 02-29-2012 at 12:46 AM.
- 02-29-2012, 12:46 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: If Statements
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.equals("Resturant"))
Note also the use of braces - these are a good idea even with one line blocks of code.Java Code:if(nameField.getText().equals("Resturant")) { System.out.println("whatever"); }
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.
- 02-29-2012, 03:18 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 27
- Rep Power
- 0
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?
- 02-29-2012, 03:24 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 27
- Rep Power
- 0
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
any suggestions on why that might be?Java Code:public void actionPerformed(ActionEvent e)
-
Re: If Statements
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.
Go through the Swing tutorials as it will show you how to create full-fledged GUI's.is there any way that you can get it to display to the applet rather than the console?
-
Re: If Statements
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.
- 03-01-2012, 12:47 PM #8
Member
- Join Date
- Nov 2011
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Help with IF Statements and GUI
By university123 in forum New To JavaReplies: 8Last Post: 10-14-2010, 04:38 AM -
if else if statements
By Allspark in forum New To JavaReplies: 9Last Post: 09-28-2010, 06:50 PM -
Help with if else statements
By np2392 in forum New To JavaReplies: 2Last Post: 09-24-2010, 01:25 AM -
if else statements
By sweetpea123 in forum New To JavaReplies: 4Last Post: 04-12-2010, 07:02 PM -
Help with if-else statements
By porchrat in forum New To JavaReplies: 4Last Post: 03-23-2009, 04:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks