0

Let's say I have a sample code, something like this

public double {method} ()
{
if (stringThatCalledThisMethod.equalsIgnoreCase("x")) // x = A String
{
return 100 ;
}
else if (stringThat....equalsIgnoreCase("y"))         // y = A String
{
return 999 ;
}

How would I call for the string that called that method? (In this instance any string can call for the method and it will always be different)

(Using this does not work as I get an error saying "Cannot find Symbol | symbol: method equalsIgnoreCase(String)

I am trying to convert a string to a double (Not sure if there is an easier way)

3
  • pass stringThatCalledThisMethod as method param double {method} (String stringThatCalledThisMethod) Commented Mar 25, 2019 at 4:50
  • double x = Double.parseDouble("1234.5678"); Commented Mar 25, 2019 at 4:51
  • 1
    You want to know the name of the method that called another method from inside the called method? Commented Mar 25, 2019 at 4:52

2 Answers 2

1

To convert a string to double, following code should do:

public double doubleString(String text){

        return Double.parseDouble(text);

}

You might want to catch NumberFormatException to handle the case of invalid value to cast as Double.

Sign up to request clarification or add additional context in comments.

Comments

0

I am guessing that you need name of the caller method and then compare it. As per my understanding this is what you need.

String callerMethodName = Thread.currentThread().getStackTrace()[2].getMethodName();

Note that you might need to replace the 2 with 1.

Other way could be sending method name as param in your 2nd method like:-

public double method2(String callerMethodName){

}

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.