Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: how to obtain the combination of ip in String??

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to obtain the combination of ip in String??

    well, i am developing a game software. now i have a problem to get the specific number from an ip. Since the ip is string in the java programming, i wanna to get the first 3 combination of ip. How should i work??
    for example, 192.168.3.122
    okay, can anyone tell me how i get 192.168.3?? Please


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: how to obtain the combination of ip in String??

    Quote Originally Posted by sxlend View Post
    ...can anyone tell me how i get 192.168.3??
    Are you looking to get a string whose value is "192.168.3" Or are you looking to get numerical values for each of the individual IP address octets?

    If it's the first one:

    Suppose you have a String whose value is, say, "192.168.16.122"
    String ipString = "192.168.16.122";


    1. Use the String lastIndexOf() method to find the index of the last '.' character in ipString.

    2. Use the String substring() method to create a new string that has everything from the beginning of ipString to the index position you just found.


    On the other hand, if you need to get the numerical values into int variables:

    1. Use the String split() method to create an array of strings, each of whose value is one of the pieces of the IP Address String:

      Now, splitting on the special character class '.' requires some regular-expression syntax that might not be exactly obvious to some of us, but it goes like this:
      String [] octetStrings = ipString.split("\\.");

      (For purposes of debugging, make a loop that prints out the elements of ipStrings here.)

    2. Each of the strings in the array can be parsed to obtain its integer value. You can create an array of ints (same size as the octetStrings array) and parse all of them in a loop.

      Or you can parse whichever ones you want as separate ints:

      For example to get the first one:
      int i0 = Integer.parseInt(octetStrings[0]);



    References:




    Cheers!


    Z
    Last edited by Zaphod_b; July 15th, 2012 at 11:29 AM.

  3. The Following User Says Thank You to Zaphod_b For This Useful Post:

    sxlend (July 15th, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to obtain the combination of ip in String??

    thanks baby

Similar Threads

  1. Printing a Combination
    By ranjithfs1 in forum Loops & Control Statements
    Replies: 2
    Last Post: March 6th, 2012, 10:24 AM
  2. A deadly combination
    By grNadpa in forum Object Oriented Programming
    Replies: 5
    Last Post: February 25th, 2012, 06:20 PM
  3. Error using a structure to obtain information of an specific array
    By andresfelquintero in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2011, 06:51 PM
  4. how to display combination of char and interger
    By bsrk315 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 22nd, 2010, 08:09 AM
  5. Replies: 1
    Last Post: February 4th, 2010, 04:23 PM