0

I have a method in the Family class

I want to add the value of the method to the JTextField from the Driver Class.

Eclipse keeps showing errors offering to make the method a string. Any help on how to do this would be great.

method

public boolean AnswerCall(String r) {
    boolean AnswerCall = true; 

    if(r == "Brother") { 
        AnswerCall = true; 
    }else {
        AnswerCall = false; 
    } 
    return AnswerCall;
}

How I am currently calling it from the driver class that is extending Family.

JTextField jt9 = new JTextField(AnswerCall());
wuno
  • 9,547
  • 19
  • 96
  • 180
  • 1
    Aside from anything else, I can't see how this has anything to do with inheritance. You should also follow Java naming conventions, and it would be clearer to just write: `return r.equals("Brother");` – Jon Skeet Mar 23 '14 at 08:36
  • 2
    JTextField doesn't have any constructor with argument `boolean` and that's why Eclipse is offering to change the return type of `AnswerCall` method to `String`. You could alway do `JTextField jt9 = new JTextField(Boolean.toString(AnswerCall()));` – AKS Mar 23 '14 at 08:38
  • I appreciate all your comments but none of that makes any sense to me. Would one of you mine elaborating or answering the question using my code so I can understand better. – wuno Mar 23 '14 at 08:40
  • There is no 'boolean object' here either, any more than there is any inheritance. The method you posted should be entirely replaced by a single String.equals() call. – user207421 Mar 23 '14 at 08:59

4 Answers4

1

try this:

public boolean AnswerCall(String r) {
    return r.equals("Brother");
}

and for textfield

String txt = Boolean.toString(AnswerCall("Sister"));//converting boolean to string value
JTextField jt9 = new JTextField(txt);
  • Ok this fixed the error. But this code just changes the value to Brother. I need it to be true if brother and false if not brother. Based on another method holding the value brother called relationship – wuno Mar 23 '14 at 09:15
  • @NichoDiaz no, it doesn't change it to brother. this code does exactly what you seem to be asking for. – Bohemian Mar 23 '14 at 09:19
  • Thank you what you have given me is working! But Yes I need to to say true or false. True if brother is in the string and false if not. what am I missing? – wuno Mar 23 '14 at 09:24
  • can u tell me how r u passing the vaules to this method? u can use this line "return r.eqaulsIgnoreCase("Brother");" to make case in-sensitive –  Mar 23 '14 at 09:26
  • Your great. thanks man I got it now. I appreciate you. I appreciate everyones help. – wuno Mar 23 '14 at 09:34
  • we are happy to help you –  Mar 23 '14 at 09:37
0

Your problem is that JTextField has not a constructor taking a boolean parameter. so when you do:

JTextField jt9 = new JTextField(AnswerCall());

The compilation will fail because AnswerCall() is retuning a boolean, while it should be return a String, so change the AnswerCall() to be return a String like:

Note: don't compare a Strings using ==, use .equals() instead

public String AnswerCall(String r) {
String answerCall = ""; 

    if(r.equals("Brother")) { 
        answerCall = "True"; 
    } 
    else {
        answerCall = "false"; 
    } 
  return answerCall;

}

Salah
  • 8,567
  • 3
  • 26
  • 43
0

You should use the method equals to compare string in java like:

public boolean AnswerCall(String r) {
    return r.equals("Brother");
}

and you can don't use a new Boolean var.

Edit:

for the constructoryou can add an empty string like this:

JTextField jt9 = new JTextField(Family().AnswerCall()+"");
CodeBird
  • 3,883
  • 2
  • 20
  • 35
0

Since you are not able to catchup with all the comments, I am going to elaborate two ways to do it:

First Way

Change your method as suggested by @Dinosaur

public boolean AnswerCall(String r) {
    return r.equals("Brother");
}

and then make the JTextField object as following:

JTextField jt9 = new JTextField(Boolean.toString(AnswerCall("your string here")));

Second Way

use the method suggested by @Salah

public String AnswerCall(String r) {
    String answerCall = ""; 
    if(r.equals("Brother"))
        answerCall = "True"; 
    else {
        answerCall = "false"; 
    return answerCall;
}

and then create the JTextField object as following:

JTextField jt9 = new JTextField(AnswerCall("your string here"));
AKS
  • 18,983
  • 3
  • 43
  • 54
  • Bother ways give me the same error. it offers to turn to string, JTextField jt9 = new JTextField(AnswerCall(ADD STUFF HERE )); – wuno Mar 23 '14 at 09:10
  • In both the cases covered here, only `String` is being passed to `JTextField`'s constructor – AKS Mar 23 '14 at 09:12