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:
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;
}
}
}
}
Re: Identifying Ints and Non Ints in a string
EDIT: removed posted because it was wrong / misleading.
Thanks for the correction Fubarable.
Re: Identifying Ints and Non Ints in a string
Quote:
Originally Posted by
SnakeDoc
Code:
int varName = Integer.getInteger(StringValue);
that will pull out any integer characters from your string and convert it into a integer variable. then you will need to do a character count on that, then add up the number of occurances.
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.
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
Quote:
Originally Posted by
Mnelson
Ok great thanks! Is there anyway you can post the code for this? I'm having trouble getting it right
You first. Let's see what you're trying.
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.
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++;
}
For example I typed in "I love this program 5"
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,
Code:
try {
Integer.parseInt(s.trim());
numInts++;
}
It's just a habit of mine after having been burned.
I'd also get rid of the continue as it's not necessary.