-1

I'm relatively new to this...

I'm trying to perform replace.all that will replace all instances of "PRICING" in a string with a numerical value which is also a variable: PRICE

SUBSCRIPTION = SUBSCRIPTION.replaceAll("PRICING",PRICE);

Below is the error i get.

Error in method invocation: Method replaceAll( java.lang.String, double ) not found in class'java.lang.String'

Thanks for your help

sTg
  • 4,313
  • 16
  • 68
  • 115

7 Answers7

2

Try this :

SUBSCRIPTION = SUBSCRIPTION.replaceAll("PRICING", String.valueOf(PRICE));
ToYonos
  • 16,469
  • 2
  • 54
  • 70
1

PRICE need to be STRING not double.

You can try this:

String price = String.valueOf(PRICE)
SUBSCRIPTION = SUBSCRIPTION.replaceAll("PRICING",price);
DevOps85
  • 6,473
  • 6
  • 23
  • 42
  • I see thanks! so maybe I should spend some more time learning about strings etc. i'm just having trouble constructing the price variable so that it is a string rather than a double? – user1629055 Nov 04 '14 at 18:31
  • @user1629055 It was a pleasure to help you :D!! you can now check the answer correct! – DevOps85 Nov 04 '14 at 18:32
  • @user1629055 - Leave `price` as `double` (especially, if you use it for calculations). – PM 77-1 Nov 04 '14 at 18:33
0

replaceAll() needs the input as,

public String replaceAll(String regex,
                String replacement)

so the replacement should be the string here , not the double as your error says,

 Error in method invocation: Method replaceAll( java.lang.String, double)

convert it into String using valueOf() method . or ,

String price= Double.toString(PRICE);

Learn more on String#replaceAll() method

Santhosh
  • 8,181
  • 4
  • 29
  • 56
0
String pricing = MessageFormat.format("{0, number, currency}", PRICE);

SUBSCRIPTION = SUBSCRIPTION.replaceAll("PRICING", pricing);

Must be a string.

Here I use the default Locale with MessageFormat.

Furthermore: BigDecimal might be a better choice than the lossy double. BigDecimal must use methods like add but double is just an approximation of a real number.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

The variable PRICE is of type double, and the method replaceAll requires second parameter to be a String. Just add the conversion:

SUBSCRIPTION = SUBSCRIPTION.replaceAll("PRICING",String.valueOf(PRICE));
0

Below code should work fine.

SUBSCRIPTION = SUBSCRIPTION.replaceAll("PRICING",String.valueOf(PRICE));
sTg
  • 4,313
  • 16
  • 68
  • 115
0

As is indicated by the error message the replaceAll() method takes two string arguments. Try using "String.valueOf(PRICE)" to convert your double to a String representation.