开发者

How to check JTextField text to String?

I have a problem on ch开发者_如何学JAVAecking 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.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜