NumberFormatException



  • Hi!

    Folgende Methode

    public MessageInfo(String msg) throws Exception {
    		String[] fields = msg.split(";");
    		if (fields.length!=2)
    			throw new Exception("MessageInfo: Invalid string for message construction: " + msg);
    		System.out.println(msg);
    		System.out.println(fields[0]);
    		System.out.println(fields[1]);
    		totalMessages = Integer.parseInt(fields[0]);
    		System.out.println(totalMessages);
    		messageNum = Integer.parseInt(fields[1]);
    		System.out.println("not anymore2");
    	}
    

    Dazu der Output:

    10;1

    10
    1

    10
    java.lang.NumberFormatException: For input string: "1
    "
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:456)
    at java.lang.Integer.parseInt(Integer.java:497)
    at common.MessageInfo.<init>(MessageInfo.java:38)
    at udp.UDPServer.processMessage(UDPServer.java:81)
    at udp.UDPServer.run(UDPServer.java:63)
    at udp.UDPServer.main(UDPServer.java:151)
    Exception in thread "main" java.lang.NullPointerException
    at udp.UDPServer.processMessage(UDPServer.java:88)
    at udp.UDPServer.run(UDPServer.java:63)
    at udp.UDPServer.main(UDPServer.java:151)

    Aus irgendeinem Grund will der den 2. String nicht richtig parsen 😞

    Hat jemand eine Ahnung warum?

    Tzugi



  • ^^da ist ein zeilenumbruch drin, der muss erstmal wech. geht mit String.trim()
    🙂



  • Integer.parseInt() might throw NumberFormatExceptions if characters other than digits and ';' are in the string. You should place them in try-blocks. Try something like this:

    public MessageInfo(String msg) throws Exception {
    int totalMessages=0, messageNum=0;
    String[] fields = msg.split(";");
    if (fields.length!=2)
    throw new Exception("MessageInfo: Invalid string for message
    construction: " + msg);
    System.out.println(msg);
    System.out.println(fields[0]);
    System.out.println(fields[1]);
    try{
    totalMessages = Integer.parseInt(fields[0]);
    }catch(NumberFormatException nfe){
    System.out.println("Substring 1 contains non-digit characters!
    Parsing...");
    totalMessages = parseInt(fields[0]);
    System.out.println("Debug: "+totalMessages);
    }
    System.out.println(totalMessages);
    try{
    messageNum = Integer.parseInt(fields[1]);
    }catch(NumberFormatException nfe){
    System.out.println("Substring 2 contains non-digit characters!
    Parsing...");
    messageNum = parseInt(fields[1]);
    System.out.println("Debug: "+messageNum);
    }
    System.out.println("not anymore2");
    }

    public int parseInt(String str)
    {
    if(str==null){
    System.out.println("String to be parsed is null! Returning 0.");
    return 0;
    }
    int count=0, x=0;
    char[] string = str.toCharArray();
    for(int i=str.length()-1; i>=0; i--){
    if(Character.isDigit(string[i]) && count>=0){
    x+=Character.getNumericValue(string[i])*Math.pow(10,count++);
    }else if(!Character.isDigit(string[i]) && count>0){
    count=-1;
    }else if(Character.isDigit(string[i]) && count<0){
    System.out.println("More than one Int in Substring!! Returning
    leftmost only.");
    return x;
    }
    }
    if(count==0)
    System.out.println("No integer found! Returning 0.");
    return x;
    }



  • System.out.println(fields[1].trim());
    

    tut's auch. Das entfernt dir alle Leerzeichen vor und nach der Zahl im String, falls da welche sind.


Log in to reply