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 2 of 2

Thread: Remove last digits of an IP address.

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Remove last digits of an IP address.

    I'm wanting to remove the last digits of an IP after the final period, i.e.

    192.168.1.2

    would be made to: 192.168.1
    similarly 192.168.1.22 would be: 192.168.1

    I'm getting the IP using the following code:
    import java.net.*;
    import java.io.*;
     
    public class Foo{
    	public static void main (String[] args){
    		try {
    				InetAddress thisIp = InetAddress.getLocalHost();
    				System.out.println(thisIp.getHostAddress());
    			} 
    				catch (Exception e)
    			{
    				e.printStackTrace();
    			}
    	}
    }
    Any ideas?

    Thanks


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Remove last digits of an IP address.

    Found a fix:


    import java.net.*;
    import java.io.*;
     
    public class Foo{
    	public static void main (String[] args){
    		try {
    				InetAddress thisIp = InetAddress.getLocalHost();
    				String addr = thisIp.getHostAddress();
    				addr = addr.substring(0,addr.lastIndexOf("."));
    								System.out.println(addr);
     
    			} 
    				catch (Exception e)
    			{
    				e.printStackTrace();
    			}
    	}
    }

Similar Threads

  1. Add or remove a row on a jtable with Netbeans 6.0
    By Pieter in forum Java Theory & Questions
    Replies: 1
    Last Post: July 8th, 2010, 03:40 PM
  2. Replies: 2
    Last Post: February 19th, 2010, 08:10 AM
  3. How to remove the in-between spaces in a String? Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 2
    Last Post: February 4th, 2010, 04:19 PM
  4. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 11:36 PM
  5. Replies: 2
    Last Post: February 4th, 2009, 12:24 PM