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.
Code :
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);
}}
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.
Code java:
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();
}
}
}