Results 1 to 7 of 7
- 07-06-2012, 04:43 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 11
- Rep Power
- 0
Identifying Ints and Non Ints in a string
Im trying to have my server report back a string that tells the client how many integers and non-integer words were in the previous message.
)For example I send the following message from my client to my server "Hi today is July 6"
I want my server to return a string "4 1"
Indicating that the incoming string has 4 non integer words and 1 integer.
Right now my server
How would I go about doing this? Here is my code:
Java Code:public class MyProgram{ public static void main(String[] args) throws Exception{ int Part = 1; DatagramSocket serverSocket = new DatagramSocket(56120); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(Part == 1) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String option= new String(receivePacket.getData()); if(option.contains("1")) { System.out.println("RECEIVED: " + option); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } else if(option.contains("2")) { System.out.println("RECEIVED: " + option); InetAddress IPAddress2 = receivePacket.getAddress(); int port2 = receivePacket.getPort(); DatagramPacket sendPacket2 = new DatagramPacket(sendData, sendData.length, IPAddress2, port2); serverSocket.send(sendPacket2); } else { System.out.println("RECEIVED: " + option); System.out.println("Exiting"); Part =0; } } } }
- 07-06-2012, 04:58 PM #2
Senior Member
- Join Date
- Apr 2012
- Posts
- 127
- Rep Power
- 0
Re: Identifying Ints and Non Ints in a string
EDIT: removed posted because it was wrong / misleading.
Thanks for the correction Fubarable.Last edited by SnakeDoc; 07-06-2012 at 06:15 PM.
-
Re: Identifying Ints and Non Ints in a string
Have you read the API for this method? Have you tested this before giving this recommendation?
Original poster, please ignore snakedoc's well-intentioned but misleading advice. The Integer.getInteger(...) will do nothing of the sort, but instead is used to get the int value associated with a certain system property. If you try to pass your String into this method it will surely return null.
Instead you will want to split your String using String's split(" ") method, using whatever delimiter is needed (here I'm assuming that it's space), and then iterate through the array returned, parsing each String token to see if its an int using Integer.parseInt(token). If so, then advance the int count, and if not, advance the non-int count.
- 07-06-2012, 06:44 PM #4
Member
- Join Date
- Jun 2012
- Posts
- 11
- Rep Power
- 0
Re: Identifying Ints and Non Ints in a string
Ok great thanks! Is there anyway you can post the code for this? I'm having trouble getting it right
-
Re: Identifying Ints and Non Ints in a string
- 07-06-2012, 09:38 PM #6
Member
- Join Date
- Jun 2012
- Posts
- 11
- Rep Power
- 0
Re: Identifying Ints and Non Ints in a string
Surley, This is what I've come up with thus far.. It seems to count the strings correctly but not the numeric s.
For example I typed in "I love this program 5"Java Code://Analyzes string for counting Int's and Non-Int's String[] stra = phrase.split(" "); int numInts = 0; int numNonInts = 0; for (String s : stra) { try { Integer.parseInt(s); } catch(NumberFormatException nfe) { numNonInts++; continue; } numInts++; }
In return It stated that it received the string but only recognized 5 NonIntegers and 0 Integers...
When in fact there was 1 number"5" in the line, Also it counts words instead of the number of characters. that isnt a huge problem but I def need to find the number of Ints in the line
-
Re: Identifying Ints and Non Ints in a string
Close. I would increment numInts inside of the try block and just under the IntegerParseInt(s). Just to be safe, I'd also parse a trimmed String,
It's just a habit of mine after having been burned.Java Code:try { Integer.parseInt(s.trim()); numInts++; }
I'd also get rid of the continue as it's not necessary.
Similar Threads
-
How to put Ints into Arrays..
By NewToJava1 in forum New To JavaReplies: 12Last Post: 04-21-2012, 10:08 PM -
How do I split a string of ints at every space
By josho493 in forum New To JavaReplies: 6Last Post: 04-18-2012, 11:45 PM -
Sum of odd ints 0-n
By falkon114 in forum New To JavaReplies: 7Last Post: 12-06-2010, 08:36 AM -
Help with ints
By Insomniac Riot in forum New To JavaReplies: 5Last Post: 04-02-2010, 03:53 PM -
checking for ints in a String
By SteroidalPsycho in forum New To JavaReplies: 1Last Post: 03-26-2010, 06:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks