Results 1 to 7 of 7
  1. #1
    Mnelson is offline Member
    Join Date
    Jun 2012
    Posts
    11
    Rep Power
    0

    Question 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;
                }
            }
    	}
    }

  2. #2
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default 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 07:15 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Identifying Ints and Non Ints in a string

    Quote Originally Posted by SnakeDoc View Post
    Java 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.

  4. #4
    Mnelson is offline Member
    Join Date
    Jun 2012
    Posts
    11
    Rep Power
    0

    Default 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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Identifying Ints and Non Ints in a string

    Quote Originally Posted by Mnelson View Post
    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.

  6. #6
    Mnelson is offline Member
    Join Date
    Jun 2012
    Posts
    11
    Rep Power
    0

    Default 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.

    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++;    
                 }
    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

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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,

    Java 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.

Similar Threads

  1. How to put Ints into Arrays..
    By NewToJava1 in forum New To Java
    Replies: 12
    Last Post: 04-21-2012, 11:08 PM
  2. How do I split a string of ints at every space
    By josho493 in forum New To Java
    Replies: 6
    Last Post: 04-19-2012, 12:45 AM
  3. Sum of odd ints 0-n
    By falkon114 in forum New To Java
    Replies: 7
    Last Post: 12-06-2010, 09:36 AM
  4. Help with ints
    By Insomniac Riot in forum New To Java
    Replies: 5
    Last Post: 04-02-2010, 04:53 PM
  5. checking for ints in a String
    By SteroidalPsycho in forum New To Java
    Replies: 1
    Last Post: 03-26-2010, 07:09 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
  •