Results 1 to 19 of 19
- 02-14-2009, 02:29 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
[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]; }
- 02-14-2009, 02:31 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Are you sure that string colStr have a number format?
- 02-14-2009, 02:35 AM #3
Error?
What is error you're getting?
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-14-2009, 02:36 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
sorry, the problem is with
and yes, if I only leaveJava Code:int row = new Integer (Integer.parseInt(rowStr));
then it is fine.Java Code:int col = new Integer (Integer.parseInt(colStr));
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.
- 02-14-2009, 02:41 AM #5
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.
- 02-14-2009, 02:44 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
First of all check that splitInput[0] and splitInput[1] values. It give the clear idea about your input string split correctly or not.
- 02-14-2009, 02:45 AM #7
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
No, the strings i parse are "3" and then "1".
sentence = "3,1"
splitInput = ["3"], ["1"]
- 02-14-2009, 02:48 AM #8
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
- 02-14-2009, 02:50 AM #9
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
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
- 02-14-2009, 03:04 AM #10
trim & prune
ah.. yes... I remember running into this before with the stupid UDP connection... try the following:
CJSLJava Code:String sentence = new String(receivePacket.getData())[B][COLOR="Blue"].trim();[/COLOR][/B]
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-14-2009, 03:06 AM #11
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
you sir, are a god.
Thanks.
- 02-14-2009, 03:07 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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(",");
- 02-14-2009, 03:08 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And this too.
Java Code:String colStr = splitInput[0]; String rowStr = splitInput[1]; int row = Integer.parseInt(rowStr); int col = Integer.parseInt(colStr);
- 02-14-2009, 03:09 AM #14
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.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-14-2009, 03:12 AM #15
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
- 02-14-2009, 03:14 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 02-14-2009, 03:15 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 02-14-2009, 03:31 AM #18
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
I'm new to java i guess
- 02-15-2009, 04:25 PM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
parseInt() vs. intValue()
By JavaPilot in forum New To JavaReplies: 5Last Post: 02-04-2009, 08:39 AM -
Integer.parseInt?
By Exhonour in forum New To JavaReplies: 4Last Post: 01-20-2009, 02:31 AM -
Need Urgent help parseInt and parseFloat problem in code need help plz
By shaggyoo7 in forum New To JavaReplies: 11Last Post: 01-10-2009, 02:16 AM -
Problem with Integer.parseInt()
By Hevonen in forum New To JavaReplies: 2Last Post: 12-14-2008, 03:41 AM -
[SOLVED] Command Line Arguments and ParseInt
By Sophiie in forum New To JavaReplies: 4Last Post: 11-16-2008, 09:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks