0

I have this java code and I want to use return to return a String from a method to a class, but I can't figure out how to use the returned String in this class:

private class NumsysAction implements ActionListener {
  public void actionPerformed(ActionEvent event) {
    String numsys_pushed = event.getActionCommand();

    if (active_numsys==2 && numsys_pushed.equals("DEC")) {
    chng_numsys_bin_dec(display.getText());

    ?????
   }


 }
}

This is the method I want to use to return a String:

public String chng_numsys_bin_dec(String chng_numsys_input) {
  String chng_numsys_output = "String I want to return";   

  return chng_numsys_output;
}

What do I have to write where the questionmarks are to put the returned String called "chng_numsys_output" into a new String variable?

2
  • You probably want to show the return as text in other component, so all you need to do is to set the text of other component as the result of chng_numsys_bin_dec invocation. You don't want to return it ;) Commented Nov 24, 2013 at 19:59
  • I am not sure what your problem is, but normally you need to assigned a return value to some variable, before you can do anything with it. So you will need something like String returnValue = chng_numsys_bin_dec(display.getText()); before the question marks. Commented Nov 24, 2013 at 20:24

2 Answers 2

1
if (active_numsys==2 && numsys_pushed.equals("DEC")) {
    String newStringVariable = chng_numsys_bin_dec(display.getText());
    // now use it
}
Sign up to request clarification or add additional context in comments.

6 Comments

What if you want to use it outside of the if?
@Makoto Outside of the if, huh? The line could also be outside of the if without any problems.
@noone, are you sure ? I am afraid your answer is wrong. Anything you declare to be inside the actionPerformed function is local to that function. The OP needs to return the value, or get the value to be used later one outside of the function.
@user3029102, you said you want to return the string. How come this worked just fine ?
@Sage: yeah, I wanted to return the string from "public String chng_numsys_bin_dec()" and use it in my "private class NumsysAction" and I was able to do that as noone described
|
0

Upon Action Event: we don't return things, we just do things by capturing some value from the Controlling component which is the source of the event. actionPerformed funciton belongs to ActionListener interface. It has void return type. If you need to capture and use the value, then use a String variable by declaring in the class context of NumsysAction. Then you will be able to use the variable by referencing it from the instance of NumsysAction class.

private class NumsysAction implements ActionListener {

    public String chngNumSysDec = "";   

  public void actionPerformed(ActionEvent event) {
    String numsys_pushed = event.getActionCommand();

    if (active_numsys==2 && numsys_pushed.equals("DEC")) {
       chngNumSysDec = chng_numsys_bin_dec(display.getText());


   }

  public String getChngNumSysBinDec() //<------ here declaring a function
  {
       return chngNumSysDec; 
  }

 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.