0

I want to put the string into one of the Integer/Float/Double, but the results into a NumberFormatException.. My string is: 37,78584, how to transform them for the type I want? First thank you for my answer.

3
  • because your String contains a space Commented Sep 14, 2012 at 3:23
  • 37785 84 is single number or two different numbers ? Commented Sep 14, 2012 at 3:23
  • @Lucifer sorry,the string is 37,78584,i'v edited Commented Sep 14, 2012 at 3:31

3 Answers 3

2

Replace Comma(",") with ""

  String s ="37,78584";
  int ival = Integer.parseInt(s.replace("," ,""));
  double dval=Double.parseDouble(s.replace("," ,""));
  float fval =Float.parseFloat(s.replace("," ,""));
Sign up to request clarification or add additional context in comments.

4 Comments

see.. number format exception will arise when converting his string "37785 84" into the single number.
@Nick if you want to convert your string into two integers then you can use 'StringTokenizer'
Agree with you, what I am saying is, like comma separated format , what if OP has stored the two different number with space ? Pls, see my comments on the question.
@Ramkiran yes now its perfect, +1 :)
2

You can do something like this first split your multiple number in String[] and loop over it and get the data.

String myString ="37785 84";
String[] mSplittedNumber= myString.split(" ");
int intValue = 0;
int doubleValue = 0;
int floatValue = 0f;

for(int i=0;i< mSplittedNumber.length;i++)
{
      intValue =Integer.parseInt(mSplittedNumber[i]);
      doubleValue =Double.parseDouble(mSplittedNumber[i]);
      floatValue =Float.parseFloat(mSplittedNumber[i]);
}

Note: If your String contains multiple number with identifier(" ","|",","....etc) else above answer will do the job

3 Comments

OP, has one single number, so i dont think we need to split the numbers.
Agree, but when I saw OP dint rectify his mistake so I proceed.
hmm... okies, no hard feelings, just to let you know that OP's req is diff, as the he forgot to add , in the que. :)
0

MyImplementation:

    String myString = "37,78584";
    {
        /* Delete whitespaces from our string */
        myString = myString.trim();

        /* Change comma to dot cause Java double/float work with dots*/
        myString = myString.replace(",", ".");  
    }
    try {
        /* Parsing */
        Double myDouble = Double.valueOf(myString);
        System.out.println(myDouble);
    } catch (NumberFormatException ex) {
        ex.printStackTrace();
    }

Hope, it was helpful for u

5 Comments

by replacing comma ( , ), to dot ( . ) ,dont you think it will return wrong value ?
I do not change the value, only divider to make the Double method work with new value with dot. It'll never return wrong value if there are only numbers and only one comma separator. Atherwise we should check the String for invalid format. It was the simplest answer, I think :)
agree, but as long as you replace , with . it will convert 3778584 to 37.78584 which is wrong it self.
I dont know that part, its OP who said that, but i am 100% sure , 3778584 is not equal to 37.78584
of course it's not equal!)) Maybe I didn't understand the task? :) What we had to do here? Thought, it was the number in string "37,78584"? which we had to convert into double/float? Am I right? :)

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.