该计划的目的是:
1.要求用户输入网址
2.检查网址是否正确
3.创建文件
4.将URL发送到文件。
基本上那么简单
但程序终止于步骤3而未执行步骤4
public static void main(String[] args) {
System.out.println("Hello there user!" );
System.out.println("What's your name?");
Scanner name=new Scanner (System.in);
String namelocation=name.nextLine();
//We will later want to impress the user by calling them by their names
System.out.println("What name would you like to give your file? Remember to keep no spaces between the letters or words");
Scanner filename=new Scanner (System.in);
String systemfilename=filename.nextLine();
//We are creating a file to store the name of the user and the browser
try {
File file = new File(systemfilename);
if (file.createNewFile()){
System.out.println("The file has been successfully created!");
}else{
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();}
System.out.println("hello" +namelocation+ "now enter the URLS of the browsers");
Scanner weblink=new Scanner(System.in);
String webpath=weblink.nextLine();
//From here we want to find out if the normal format of writing URLS has been followed e.g does it begin with https:, and .com
int numberofcharacters=webpath.length();//we will use this later on to get the final position of the last substring
String linklength=webpath.substring(0,7);
if (linklength.equals("https:")){
System.out.println("Good so far");
}
String linklength1=webpath.substring(0,4);
if(linklength1.equals("www.")){
System.out.println("Good so far");
}
else{
System.out.println("Incorrect link!!!");
}
//Now we are getting the position of the last substring
int linklength2=numberofcharacters-4;
int linklength3=numberofcharacters+1;
String linklength4=webpath.substring(linklength2,linklength3);
if(linklength4.equals(".com")){
System.out.println("Great!!!");
}
if(linklength4.equals(".edu")){
System.out.println("Great!!!");
}
if(linklength4.equals(".gov")){
}
else{
System.out.println("Something might not be right with this link");
}
String content = webpath;
PrintStream out = null;
//this is where we print the URL to the file
try {
out = new PrintStream(new FileOutputStream(content));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.setOut(out);
// TODO Auto-generated method stub
}
} }
答案 0 :(得分:0)
您没有正确关闭catch语句。 try / catch块需要采用以下格式:
try {
// stuff to try
} catch {
// do stuff in this case
}
您的try / catch块缺少catch块中的右括号。你有:
} catch (IOException e) {
e.printStackTrace();
旁注:这是一个开口括号 - > {
这是一个结束括号 - > }
。无论何时使用开口支架,都要注意结束支架的位置。
因为你永远不会关闭你的catch块,所以它下面的所有内容都包含在catch块中。含义... url检查程序代码运行的唯一方法是文件创建失败...
将您的catch块更改为:
} catch (IOException e) {
e.printStackTrace();
}
您的IDE最不会抛出错误,因为您刚刚在代码的最后添加了一个额外的结束括号。我假设你帖子最后的双括号是什么。