Results 1 to 15 of 15

Thread: Guess Character

  1. #1
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Question Guess Character

    hey guyz , ive been tryin to write an applet to get characters from A-E from user , if the character matches the character from random , then it displays u got , else , u lose . however i got couple of errors , would u help me on this?
    heres the code :

    Java Code:
    import java.util.Random; 
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    public class Guesschar extends Applet implements ActionListener{
    
    	Label guess,en,result;
    	TextField blank;
    	
    	public void init() {
    		guess = new Label(" Guess a letter !");
    		en = new Label("Enter Letter from A-E");
    		blank = new TextField(5);
    		result = new Label("'The RESULT WILL be..'");
    		add(guess);
    		add(en);
    	
    		
    		blank.addActionListener(this);
    		add(blank);
    		add(result);
    		
    	}
    	public void actionPerformed(ActionEvent e){
    		
    	 	Random	ran = new Random();
    		String r = ran.nextChar(10);
    		
    		if (e.getSource()==blank){
    			String x = String.parseString(blank.getText());
    			
    			if  (x==r)
    				result.setText("You Win! Number is " + r);
    				else
    				result.setText("You Lose! Number is " + r);
    			
    			
    			
    		}
    	}
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Never compare Strings using ==, use equals()
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    830
    Rep Power
    6

    Default

    what is thjis parseString and nextchar meth...see the api.....dont try ur own...
    Ramya:cool:

  4. #4
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    gimme some clue ! im confused ...

  5. #5
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    any idea on this guyz ?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    First off we don't know what's wrong since you haven't told us.
    Second, you have been given some pointers to possible problems...but you haven't said if they've helped.

  7. #7
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    i know how to do this program for number " integers " . the problem is that i don know how to do the same for letters "characters " . i know the foundation is the same ,just need to change the integers to characters , n i don know how to do that . btw given solutions by others havent helped me so far .
    tnx for ur help. :)

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    What did you do about this?
    Quote Originally Posted by PhHein View Post
    Never compare Strings using ==, use equals()
    If you fixed it, show us your new code. If you haven't fixed it, then fix it.

    -Gary-

  9. #9
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    alright i did the change , is it correct ?

    Java Code:
    import java.util.Random; 
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    public class Guesschar extends Applet implements ActionListener{
    
    	Label guess,en,result;
    	TextField blank;
    	
    	public void init() {
    		guess = new Label(" Guess a letter!");
    		en = new Label("Enter Letter A-E");
    		blank = new TextField(5);
    		result = new Label("'The RESULT WILL be..'");
    		add(guess);
    		add(en);
    	
    		
    		blank.addActionListener(this);
    		add(blank);
    		add(result);
    		
    	}
    	public void actionPerformed(ActionEvent e){
    		
    	 	Random	ran = new Random();
    		char r = ran.nextChar(10);
    		
    		if (e.getSource()==blank){
    			char x = String.parseChar(blank.getText());
    			
    			if  (x.equals(r)){
    				result.setText("You Win! Number is " + r);
    				else
    				result.setText("You Lose! Number is " + r);
    			
    			
    			
    		}
    	}
    }

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    Does it work?
    We're not going to test your code for you...

  11. #11
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    no it aint working ! what should i do ?

  12. #12
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    What doesn't work? What error messages do you get?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  13. #13
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    one question , does this if statement look alright to ya'll ?!

    Java Code:
    public void actionPerformed(ActionEvent e){
    		
    	 	Random	ran = new Random();
    		char r = ran.nextChar(10);
    		
    		if (e.getSource()==blank){
    			char x = String.parseChar(blank.getText());
    			
    			if  (x.equals(r)){/*<< specifically this line*/
    				result.setText("You Win! Number is " + r);
    				else
    				result.setText("You Lose! Number is " + r);
    			
    			
    			
    		}

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    Are you getting a error?

    Since both x and r are chars I would expect an error (unless auto-boxing kicked in possibly?)...but you could simply do x == r, since char is a primitive. You want to use equals() with objects and == with primitives.

  15. #15
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    I don't think Java's Random class has a nextChar() method, and even if it did, I doubt that nextChar(10) would do what you want there.

    -Gary-

Similar Threads

  1. resultset.next() = false ;; Can't Guess why.. please help.
    By Stephen Douglas in forum New To Java
    Replies: 11
    Last Post: 04-12-2010, 10:21 AM
  2. Help about Guess the Numbers Program in java
    By macfrik in forum New To Java
    Replies: 6
    Last Post: 03-25-2009, 03:59 AM
  3. guess number game
    By mistah in forum New To Java
    Replies: 10
    Last Post: 11-23-2008, 03:37 AM
  4. sample of guess high and low game
    By pouria62 in forum AWT / Swing
    Replies: 1
    Last Post: 10-26-2008, 12:57 PM
  5. reading text character by character
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 08:54 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
  •