开发者

ParseInt raises exception when reading numeric output of StringBuffer

开发者 https://www.devze.com 2023-03-26 15:52 出处:网络
My application reads the output of a command line programs which is always a 0-3 digit number. The data starts as a stringBuffer, is sent .toString and the third step is to convert it to an integer fo

My application reads the output of a command line programs which is always a 0-3 digit number. The data starts as a stringBuffer, is sent .toString and the third step is to convert it to an integer for comparison. The final step using Integer.parseInt raises a java.lang.NumberFormatException. For example, if the output of the command line program is "0", this is the exception.

08-08 18:31:30.167: ERROR/AndroidRuntime(26484): FATAL EXCEPTION: main
08-08 18:31:30.167: ERROR/AndroidRuntime(26484): java.lang.NumberFormatException: unable to parse '0
08-08 18:31:30.167: ERROR/AndroidRuntime(26484): ' as integer
08-08 18:31:30.167: ERROR/AndroidRuntime(26484):     at java.lang.Integer.parse(Integer.java:433)

Notice the linebreak around '0'. I know conversion can be tricky and I've checked the charset and explicitly set it to UTF-8. Also I've tried to check for '\n' or other non numeric characters via this code before the conversion:

System.out.println(score);
    for(char c: score.toCharArray()){
        System.out.println(c);
    }

This code may not sufficiently reveal the fault. However, if the program is left to run without the integer conversion, each single numeral number is printed as itself, and each multi-digit number is printed with a digit on each line without any extraneous characters.

I'm fairly certain that I'm trying to convert the pertinent data in addition to some extra invisible characters.

  1. How can I figure out what these invisible characters are?
  2. What is the best way to get rid of them, and get an integer representation of only the numberic output of the subprocess?

For Reference: The code of the subprocess call:

try{    
        Process subProc = Runtime.getRuntime().exec("data/data/com.app/bin/prog /sdcard/file");

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(subProc.getInputStream(), Charset.defaultCharset()), 10);
        int read;
        char[] buffer = new char[10];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        reader.close();
        subProc.waitFor();
        score = output.toString();


    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (Interrupt开发者_如何学CedException e) {
        throw new RuntimeException(e);
    }


    System.out.println(score);
    for(char c: score.toCharArray()){
        System.out.println(c);
    }
    return score;

And for kicks, the ParseInt function that raises the exception:

int score = Integer.parseInt(NativeClass.getScore(file));

Thanks in advance.


  1. You can check numeric representation of every character you're trying to parse. You can use static int Character.digit(char ch, int radix) method. Everything that is out of the range of numerical representation of 0-9 is your "bad" character.
  2. String.replaceAll("[^0-9]+", "") will remove everything non-digit from your input, including \n symbols.


If the 'invisible characters' are white space, just use the String.trim() method to get rid of those. If there is a possibility of other characters in your input, you will need to use regular expressions to extract only the numbers.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号