0

因此,我应该编写一个读取 URI 的程序,例如http://www.cs.unca.edu/csci202,包含在第一个输入行中。如果构造函数引发异常,程序应该打印一条消息,提供有关该问题的一些信息。如果没有引发异常,程序应该使用 URI 的适当方法打印 URI 的方案、主机和路径。如果缺少这些 URI 属性中的任何一个,则不应打印它。我应该使用扫描仪来输入 URI,然后让它打印出方案主机路径或错误消息。到目前为止,我尝试过的任何事情都没有奏效。这是我到目前为止所拥有的:

import java.net.URI;

public class Home04 {

    public static void main(String[] args) {

        try {
            URI uri1 = new URI("");

            if (uri1.getScheme() != null) {
                System.out.println("Sceme:" + uri1.getScheme() );
            }
            if (uri1.getHost() !=null){
                System.out.println("Host:" + uri1.getHost() );
            }
            if (uri1.getPath() != null){
                System.out.println("Path:" + uri1.getPath() );
            }
        } catch (Exception e) {
            System.out.println("Exception" + e);
        }
    }
}
4

1 回答 1

0

Your parsing code is correct.

What you need to do is use below to get input from user

Scanner scanner = new Scanner(System.in);
URI uri1 = new URI(scanner.next());
于 2013-02-15T21:26:12.180 回答