Question

I have a string s which contains :

10011111 10011111 10011111 10011111 10011111 10011111 10011111
10011111 10011111 10011111 10011111 10011111 10011111 10011111

10011111 10011111 10011111 10011111 10011111 10011111 10011111
10011111 10011111 10011111 10011111 10011111 10011111 10011111

10011111 10011111 10011111 10011111 10011111 10011111 10011111
10011111 10011111 10011111 10011111 10011111 10011111 10011111

10011111 10011111 10011111 10011111 10011111 10011111 10011111
10011111 10011111 10011111 10011111 10011111 10011111 10011111

Now I used twice method split. First I made couples = split("\n\n") to have array of rows. Now I wanted to take every value from a row so I made: values = couples[i].split(" "). And I wanted to use for every values[i] code:

(byte) Integer.parseInt(values[i], 2);

This works fine, but only for values which are in couples[0]....couples[2]. For the values (the last value) in couples[3] I have error "java.lang.NumberFormatException". I thought that this is connected with the end of file. But I don't know how to fix that.

Était-ce utile?

La solution

For your second split, use couples[i].split("\\s+") so that you split on any type of whitespace. Otherwise, the newline character gets in the way.

Autres conseils

Because you have two consecutive new lines after every other line of input, every 3rd entry in your couples[] will be empty. Just check to make sure the string isn't empty before parsing.

if( values[i] != null && !values[i].isEmpty() )
    (byte) Integer.parseInt( values[i], 2 );
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top