Integer.parseInt("0") fails

Discussion in 'Plugin Development' started by V10lator, Feb 10, 2012.

Thread Status:
Not open for further replies.
  1. Hi,
    I'm parsing the reply from a webside. Parsing the header and splitting header and body works, but then it fails.
    The Webside itself just shows things like:
    Code:
    0
    1 bla
    2
    3 foo
    and that's the parsing code:
    Code:java
    1. String[] split;
    2. if(body.contains("\n"))
    3. split = body.split("\n");
    4. else
    5. split = new String[] { body };
    6. int[] cmds = new int[split.length];
    7. try
    8. {
    9. for(int i = 0; i < split.length; i++)
    10. {
    11. if(split[i].equals(""))
    12. continue;
    13. System.out.print("Checking: '"+split[i]+"'");
    14. if(!split[i].contains(" "))
    15. {
    16. cmds[i] = Integer.parseInt(split[i]);
    17. split[i] = null;
    18. }
    19. else
    20. {
    21. sp = split[i].indexOf(" ");
    22. cmds[i] = Integer.parseInt(split[i].substring(0, sp));
    23. split[i] = split[i].substring(sp + 1);
    24. }
    25. }
    26. }
    27. {
    28. System.out.print("Check: Invalid command");
    29. return;
    30. }[/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]

    In the logs I get:
    Code:
    Checking: '0'
    Check: Invalid command
     
  2. Offline

    nisovin

    Do you maybe have a \r in there? Try printing
    Code:
    split[i].length()
     
    dannycrafts likes this.
  3. Offline

    dannycrafts

    I was thinking the same, maybe you have an \r\n, \n or \r in there.
     
  4. Not possible as I filter out all \r:
    reply = reply.replaceAll("\r", "");
    and split by \n:
    Code:java
    1.  
    2. if(body.contains("\n"))
    3. split = body.split("\n");
    4. else
    5. split = new String[] { body };

    But the length test goes in the right direction: I extended the debugging line:
    Code:java
    1. System.out.print("Checking: '"+split[I]+"': "+split[I].length());[/I][/I]

    And the output:
    Checking: '0': 776
    Why the hell 776?!


    //EDIT: Got it! The bad character was \0
     
  5. Offline

    Njol

    Why don't you simply use split = body.split("\n")?
     
Thread Status:
Not open for further replies.

Share This Page