0

I have a problem on checking the text and string.

public boolean Isequals(Object c){
    boolean check = false;
    if (c instanceof MyTextTwist){
        MyTextTwist tt = (MyTextTwist)c;
    if (txtGuessPad.getText().equals(answer))
        check = true;}
    return check;
    }

this what i have so far.

Denver
  • 147
  • 3
  • 17
  • what is txtGuessPad? and what is MyTextTwist? – Neifen Sep 22 '11 at 10:17
  • 1
    By reading this question, you may feel like you're reading an ancient egyption wall or something..from my experience in such things I guess the question is about Comparing The JTextField contents with another string. – Mohamad Alhamoud Sep 22 '11 at 10:17
  • Error when i click the submit button? do i have to use the txtGuesspad.getText().equals(answer)? – Denver Sep 22 '11 at 10:20
  • 1
    @M.H that what it is. Sorry but im not very good in english :( – Denver Sep 22 '11 at 10:20
  • @HilverBomb The problem is not about your english, but you must explain more details so we can help you. What is "MyTextTwist"?? What is "answer" ? and What is The error...etc – Mohamad Alhamoud Sep 22 '11 at 10:29
  • The answer contains the word which is i want to compare with the word input by the user, Im creating a texttwist and the MyTextTwist is my constructor. the error is when i click the submit button it doesn't do anything – Denver Sep 22 '11 at 10:32
  • What are you expecting from the submit button to do ? your "if" condition seems to be correct.Also is there a compiler error in your code ? – Mohamad Alhamoud Sep 22 '11 at 10:38
  • if the the text and the answer is the same i want it to return true. i just wondering if the gettext().equals is correct? – Denver Sep 22 '11 at 10:48
  • @HilverBomb Yes it is correct. – Mohamad Alhamoud Sep 22 '11 at 11:07

1 Answers1

1

Since your question is not very clear, i suggest these answers:

1st option - you want to get string from your JTextField:

String text = txtGuessPad.getText();

2nd option - you want to check if text contains only letter:

String text = txtGuessPad.getText();
if(text.matches("^[a-zA-Z]+$")){...

3rd option - you want to compare two strings (one of them is from JTextField):

String text = txtGuessPad.getText();
String text2 = "test";
if(text.equals(text2)){... //if you want to match whole word and case sensitive
if(text.equalsIgnoreCase(text2)){... //if you want to match whole word and NOT case sensitive
if(text.startsWith(text2)){... //if you want to check if you string starts with other string

4th option - let's put it in a function:

public boolean isEqualToString(JTextField textField, String compareTo) {
     String text = textField.getText();
     if(text.equals(compareTo)) {
         return true;
     }
     return false;
}
Minutis
  • 1,193
  • 1
  • 17
  • 47
  • public class SubmitHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ String text = txtGuessPad.getText(); String text2 = answer; int score = 10; if(text.equalsIgnoreCase(text2)){ txtscore.setText(score);}//error here :( } } – Denver Sep 22 '11 at 11:05