我正在尝试使用InetAddress返回用户输入的网站名称的IP地址,但我在声明中收到错误: InetAddress ip = new InetAddress.getByName(site); 显示的错误是:
InetAddress.getByName cannot be resolved to a type
我的代码:
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.");
}
}
}
答案 0 :(得分:5)
摆脱新的'。这是一种静态方法。
答案 1 :(得分: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.");
}
}
}
只是一个新的'那不应该在这里;)
答案 2 :(得分:2)
删除方法调用前的新内容,如下所示:
InetAddress ip = InetAddress.getByName(site);