Results 1 to 19 of 19
  1. #1
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Default [SOLVED] Integer.parseInt() problem

    hi chaps. im having a bit of a problem with Integer.parseInt()

    at run time, there is an error with line "int col = new Integer (Integer.parseInt(colStr));"
    the field sentence is "x,y" x and y being two numbers.

    Any help would be great,

    code below:

    Java Code:
    while(true)
    		{
    			DatagramPacket receivePacket = new DatagramPacket(
    				receiveByte, receiveByte.length);
    			serverSocket.receive(receivePacket);
    			InetAddress IP = receivePacket.getAddress();
    			String sentence = new String(receivePacket.getData());
    			int port = receivePacket.getPort();
    			// data manipulated here:
    			String[] splitInput = (String[]) sentence.split(",");
    
    			// BUG: can't parseInt on splitInput[1]
    			String colStr = new String (splitInput[0]);
    			String rowStr = new String (splitInput[1]);
    			int row = new Integer (Integer.parseInt(rowStr));
    			int col = new Integer (Integer.parseInt(colStr));
    
    			board.updateBoard(col, row);
    			//String capsSentence = sentence.toUpperCase();
    
    			// TODO something useful with coordinates
    			newCoords = (sentence);
    			System.out.println("reply: " + newCoords);
    			sendByte = newCoords.getBytes();
    
    			DatagramPacket sendPacket = new DatagramPacket(
    				sendByte, sendByte.length, IP, port);
    			serverSocket.send(sendPacket);
    			receivePacket = null;
    			sendPacket = null;
    			sentence = null;
    			newCoords = null;
    
    			// clear any data in byte
    			receiveByte = new byte[1024];
    			sendByte = new byte[1024];
    
    		}

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Error?

    What is error you're getting?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Default

    sorry, the problem is with

    Java Code:
    int row = new Integer (Integer.parseInt(rowStr));
    and yes, if I only leave
    Java Code:
    int col = new Integer (Integer.parseInt(colStr));
    then it is fine.

    There seems to be a problem with the second part of the "sentence" field.
    no matter what the y part of the sentence is, it won't convert to type int.

  5. #5
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    is your string that you parse "3,1"? If so that's your problem. the comma, it isn't a number.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all check that splitInput[0] and splitInput[1] values. It give the clear idea about your input string split correctly or not.

  7. #7
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Default

    No, the strings i parse are "3" and then "1".
    sentence = "3,1"
    splitInput = ["3"], ["1"]

  8. #8
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    First of all check that splitInput[0] and splitInput[1] values. It give the clear idea about your input string split correctly or not.
    done that,
    System.out.println(splitInput[0]); = 3
    System.out.println(splitInput[1]); = 1

  9. #9
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    What is error you're getting?

    CJSL
    error when sentence = "3,1" is:

    Exception in thread "main" java.lang.NumberFormatException: For input string: "1"


    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:456)
    at java.lang.Integer.parseInt(Integer.java:497)
    at networkUDP.udpserver.main(udpserver.java:35)
    Java Result: 1

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default trim & prune

    ah.. yes... I remember running into this before with the stupid UDP connection... try the following:

    Java Code:
    String sentence = new String(receivePacket.getData())[B][COLOR="Blue"].trim();[/COLOR][/B]
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Default

    you sir, are a god.

    Thanks.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I cannot see number format exception on your code.

    One more thing, no need to use redundant cast here.

    Java Code:
    String[] splitInput = (String[]) sentence.split(",");

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    And this too.

    Java Code:
    String colStr = splitInput[0];
    			String rowStr = splitInput[1];
    			int row = Integer.parseInt(rowStr);
    			int col = Integer.parseInt(colStr);

  14. #14
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default no problem...

    You're welcome... it really took me a while to figure it out when I ran into it and I haven't seen it mentioned in any books either.
    Glad I could help.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  15. #15
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    I cannot see number format exception on your code.

    One more thing, no need to use redundant cast here.

    Java Code:
    String[] splitInput = (String[]) sentence.split(",");
    thanks, I left that there when i tried to force it being a string. :o

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    So that means this is wrong.

    Quote Originally Posted by eggmanpete View Post
    error when sentence = "3,1"
    It should be,

    Quote Originally Posted by eggmanpete View Post
    error when sentence = "3,1 "
    additional space at the end. :rolleyes:

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by eggmanpete View Post
    thanks, I left that there when i tried to force it being a string. :o
    split() always return a string array lol. ;)

  18. #18
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Default

    I'm new to java i guess

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Never mind lol. You can learn a lot form our community, a lot.

    At the same time, if you have found the solution here for your question please mark the thread as solved. It's really helpful to all other members who are commenting and referring your question.

Similar Threads

  1. parseInt() vs. intValue()
    By JavaPilot in forum New To Java
    Replies: 5
    Last Post: 02-04-2009, 09:39 AM
  2. Integer.parseInt?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 03:31 AM
  3. Replies: 11
    Last Post: 01-10-2009, 03:16 AM
  4. Problem with Integer.parseInt()
    By Hevonen in forum New To Java
    Replies: 2
    Last Post: 12-14-2008, 04:41 AM
  5. [SOLVED] Command Line Arguments and ParseInt
    By Sophiie in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 10:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •