1

I am attempting to turn an array list of strings into an arraylist of floats. I have declared the two as such:

ArrayList<Float> AFFloat = new ArrayList<Float>();
ArrayList<String> AFUni0 = new ArrayList<String>();

AFUni is an array list that was parsed from a file. It holds values such as:

[0.059, 0.059, 0.029, 0.412, 0.029, 0.452, 0.386, 0.432, 0.114,0.318, 0.159,0.045, 0.432, 0.477, 0.045...]

I am trying to make those string values into actual numeric values with this set of code:

for (String wntFl:AFUni0){
  AFFloat.add(Float.valueOf(wntFl));
}

But for some reason it isn't working. It is coming back with this error:

java.lang.NumberFormatException: For input string: "0.114,0.318"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
    at java.lang.Float.valueOf(Float.java:388)
    at allelefreq.AlleleFreq.main(AlleleFreq.java:122)

Does anyone know why this is happening? Thanks

1
  • 2
    That String value contains two float values, you didn't do the splitting correctly, it seems. Commented Sep 24, 2012 at 13:54

8 Answers 8

3

It looks like some values are separated by ", " while others, like your example, only by ",". This could be the reason for the failed recognition.

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

Comments

2

The answer is in your Exception

java.lang.NumberFormatException: For input string: "0.114,0.318"

You pass the "0.114,0.318" as wntFl, that is why you have NUmberFormatException

You should assure that your input is valid.

First you should split the input, then you can parse it.

Comments

1

As the error message shows you, the value it's failing on is "0.114,0.318" (i.e., NOT "0.114" or "0.318", but multiple numbers in one String), which is not a valid number. However you're populating your ArrayList, you're getting multiple values in a single String. You can fix this by fixing the code that populates the array or using String.split(",") to get an array of the values and loop over that before casting to floats.

Comments

0

0.114,0.318 is not a valid float (Observe ,in between), that is whyNumberFormatException`

You may need to first split() the string which returns array and then pass values to Float.valueOf(str[i])

1 Comment

Ah yes, that would explain it. I am not sure how I overlooked that. Thanks!
0

Separators are not quite constant in all string-stream. Try to find problem in this way.

Comments

0

you should use String.split(",") because 0.114,0.318 is not a valid float

Comments

0

You don't do the splitting correctly. This kind of project works very well (but its just example)

public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<Float> floats = new ArrayList<Float>();
        ArrayList<String> strings = new ArrayList<String>();

        strings.add("0.123");
        strings.add("1.123");
        strings.add("0.423");
        strings.add("34.423");
        strings.add("0.000");

        for(String str : strings) {
            floats.add(Float.valueOf(str));
        }

        for(Float fl : floats) {
            System.out.println(fl);
        }

    }

Comments

0

This could be the reason of Internationalization/Localization, In many languages . is replaced with ,/،

Just like in Spanish http://translate.google.com/#auto/es/10.1

Or in Arabic http://translate.google.com/#auto/ar/10.1.

You have to take care of localization in case of string to float conversion.

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.