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: wanting 2nd octet of an IP address

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

    Default wanting 2nd octet of an IP address

    I don't know enough java, so I am probably doing this wrong. What I would like to have, is a String that has the second octet of an IP address, weather it is an 8 or 58 or 103, just as an example.

    import java.net.*;
    import java.io.*;
    import javax.swing.JOptionPane;
    public class findip {
    public static void main(String [] args) throws IOException{
    	String myip,myip2;
    	InetAddress thisIp =InetAddress.getLocalHost();
    	//System.out.println("IP of my system is := "+thisIp.getHostAddress());
    	myip=thisIp.getHostAddress();
    	myip2=myip.charAt(3)+myip.charAt(4);
    	//need to just show or have 2nd octet
    	JOptionPane.showMessageDialog(null,"IP:__"+myip+"\n\nSchool Num:__"+myip2);
    }}


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: wanting 2nd octet of an IP address

    It's not an octet. It's decimal. This example here shows the last part of an IP. You can modify it to get the 1st, 2nd or 3rd part.
    1st: myIP.substring(0, myIP.indexOf(".");
    2nd: myIP.substring(myIP.indexOf(".")+1, myIP.indexOf(".", myIP.indexOf(".")+1));
    etc.
    import javax.swing.JOptionPane;
    import java.net.InetAddress;
    public class WhatIP {
    	public static void main(String[] args) {
    		try {
    			InetAddress ia = InetAddress.getByName(args[0]);
    			String myIP = ia.toString();
    			JOptionPane.showMessageDialog( null, myIP+"last Part = "+myIP.substring(myIP.lastIndexOf('.')+q, myIP.length())); 
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
      	}
    }

Similar Threads

  1. Replies: 1
    Last Post: April 18th, 2012, 01:12 PM
  2. Wanting to repeat my program...
    By Shaybay92 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 8th, 2011, 08:29 AM
  3. Created simple game applet but wanting it to access a DSN on the server
    By hirsty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 21st, 2011, 03:11 AM
  4. wanting to convert printout to UPPER case
    By welikedogs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 3rd, 2010, 01:22 PM
  5. Um, ya, wanting to do a little more than my program can take
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2010, 04:36 PM