0

I am trying to use InetAddress to to return the IP address of a website name that the user enters, but I get an error at the statement: InetAddress ip = new InetAddress.getByName(site); The error shown is :

InetAddress.getByName cannot be resolved to a type

My code :

import java.util.*;
import java.net.*;
import java.io.*;
import java.net.InetAddress;

public class getIP {
    public static void main(String args[])throws UnknownHostException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String site;
        System.out.println("Enter the url :");
        site = br.readLine();
        try
        {
            InetAddress ip = new InetAddress.getByName(site);
        }
        catch(UnknownHostException ee)
        {
            System.out.println("Website not found.");
        }

    }
}
user3883991
  • 71
  • 2
  • 6

3 Answers3

5

Get rid of the 'new'. It's a static method.

user207421
  • 305,947
  • 44
  • 307
  • 483
2
import java.util.*;
import java.net.*;    
import java.io.*;
import java.net.InetAddress;

public class getIP {
public static void main(String args[])throws UnknownHostException 
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String site;
    System.out.println("Enter the url :");
    site = br.readLine();
    try
    {
        InetAddress ip = InetAddress.getByName(site);
    }
    catch(UnknownHostException ee)
    {
        System.out.println("Website not found.");
    }

}
}

Just a 'new' that shouldn't be here ;)

Valentin Montmirail
  • 2,594
  • 1
  • 25
  • 53
2

Remove the new in front of the method call, like so:

InetAddress ip = InetAddress.getByName(site);
janwschaefer
  • 609
  • 3
  • 6