0

I have a spinner for Period which contains the selections:

3 months
6 months
10 months
12 months
18 months 

PROBLEM:

I can get the value of the spinner selection but I need a way to get just the numerical value of the selection ( i.e. just the 3 from 3 months ) for use in another activity with calculations using BigDecimal. Can anyone guide me accordingly?

Cai
  • 5,063
  • 3
  • 18
  • 23

1 Answers1

0

The code below will replace all non digits and you will get inly integer value.

String s = spinner.getSelectedItem().toString();

int spinnerVal = Integer.parseInt(s.replaceAll("[\\D]", ""))
Apurva
  • 7,871
  • 7
  • 40
  • 59
  • Found the same answer [here](http://stackoverflow.com/questions/10372862/java-string-remove-all-non-numeric-characters) the same time you answered haha, I'll accept your answer as soon as that accepting timer goes, thanks :) – Cai Mar 15 '15 at 15:33
  • 1
    @Astatine0936 no the answers are **not** same!! But the output will be same indeed. The answer link you have posted says, `replaceAll("[^\\d.]", "");` where `^` means `not` and `d` means `digits` while my answer is `replaceAll("[\\D]", ""` where `D` itself means **`non digit`**. So the answer is NOT a copy. – Apurva Mar 15 '15 at 15:41
  • Ahh, so that's what the lower and upper case stand for. Thank you very much for the clarification, I will definitely remember that in the future. In any case I used your code above anyway so everything is working perfect now. Again thank you very much :) – Cai Mar 15 '15 at 15:51
  • 1
    Here's [regex cheat sheet](http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/) which will help you a lot for such kinda solutions. – Apurva Mar 15 '15 at 15:53