如何从Java中的URL计算方案和域
这是一个学校作业,我必须接受一个命令行参数,该参数包含包含URL的文件。如果没有命令行参数,我必须接受标准输入的参数。然后,我必须报告这些URL中存在多少类型的特定域和方案,以及报告每个文件中读取了多少行。程序应该读取行,直到到达“结束”。我没有得到我应该得到的正确输出。它应该输出正确的显示次数计数。例如,如果通过命令行输入一个列表,其中包含“ 结束“它应该报告回来 “>>找到了https的一个实例”“>>找到了ftp的一个实例”“>>找到了 com的一个实例“>>找到了gov的一个实例” 然而,我认为计数或打印部分可能有问题如何从Java中的URL计算方案和域,java,Java,这是一个学校作业,我必须接受一个命令行参数,该参数包含包含URL的文件。如果没有命令行参数,我必须接受标准输入的参数。然后,我必须报告这些URL中存在多少类型的特定域和方案,以及报告每个文件中读取了多少行。程序应该读取行,直到到达“结束”。我没有得到我应该得到的正确输出。它应该输出正确的显示次数计数。例如,如果通过命令行输入一个列表,其中包含“ 结束“它应该报告回来 “>>找到了https的一个实例”“>>找到了ftp的一个实例”“>>找到了 com的一个实例“>>找到了gov的一个实例” 然而
import java.io.*;
import java.util.*;
import java.io.*;
import java.util.Scanner;
import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
public class P1{
public static void main(String[] args) throws FileNotFoundException, MalformedURLException{
int httpCount=0;
int httpsCount=0;
int ftpCount=0;
int schemeOtherCount=0;
int eduCount=0;
int orgCount=0;
int comCount=0;
int domainOtherCount=0;
int lineCount=0;
int fileLineCount = 0;
int totalLineCount = 0;
String fileName = " ";
outerloop:if(args.length > 0){
for(int i = 0; i < args.length; i++){
fileName = args[i];
fileLineCount = 0;
File theBestFileEver = new File(fileName);
Scanner fileScan = new Scanner(theBestFileEver);
while(fileScan.hasNextLine()){
String fileHolder = fileScan.nextLine();
if(fileHolder.equals("end")){
break outerloop;
}
URL argsURL = new URL(fileHolder);
String almostDomain = argsURL.getHost();
int lastPeriod = almostDomain.lastIndexOf(".");
int end = almostDomain.length();
String argsDomain = almostDomain.substring(lastPeriod+1, end);
String argsScheme = argsURL.getProtocol();
if(argsScheme.equals("http"))
httpCount++;
if(argsScheme.equals("https"))
httpsCount++;
if(argsScheme.equals("ftp"))
ftpCount++;
if(!argsScheme.equals("http") && !argsScheme.equals("https") && !argsScheme.equals("ftp"))
schemeOtherCount++;
if(argsDomain.equals("edu"))
eduCount++;
if(argsDomain.equals("org"))
orgCount++;
if(argsDomain.equals("com"))
comCount++;
if(!argsDomain.equals("edu") && !argsDomain.equals("org") && !argsDomain.equals("com"))
domainOtherCount++;
fileLineCount++;
totalLineCount++;
if(fileLineCount == 1)
System.out.println(">> Got " + fileLineCount + " line from " + fileName);
if(fileLineCount != 1)
System.out.println(">> Got " + fileLineCount + " lines from " + fileName);
}
}
}
else{
Scanner scanTwo = new Scanner(System.in);
here: while(scanTwo.hasNextLine()){
String scannedUrlTwo = scanTwo.nextLine();
if(scannedUrlTwo.equals("end")){
break here;
}
URL twoURL = new URL(scannedUrlTwo);
lineCount++;
String scheme = twoURL.getProtocol();
String nearlyDomain = twoURL.getHost();
int twoLastPeriod = nearlyDomain.lastIndexOf(".");
int twoEnd = nearlyDomain.length();
String domain = nearlyDomain.substring((twoLastPeriod+1) , twoEnd);
if(scheme.equals("http"))
httpCount++;
if(scheme.equals("https"))
httpsCount++;
if(scheme.equals("ftp"))
ftpCount++;
if(!scheme.equals("http") && !scheme.equals("https") && !scheme.equals("ftp"))
schemeOtherCount++;
if(domain.equals("edu"))
eduCount++;
if(domain.equals("org"))
orgCount++;
if(domain.equals("com"))
comCount++;
if(!domain.equals("edu") && !domain.equals("org") && !domain.equals("com"))
domainOtherCount++;
if(lineCount == 1)
System.out.println(">> Got 1 line from standard input");
if(lineCount != 1)
System.out.println(">> Got " + lineCount + " lines from standard input");
}
}
if(httpCount != 1)
System.out.println(">> Found " + httpCount + " instances of http");
if(httpCount == 1)
System.out.println(">> Found " + httpCount + " instance of http");
if(ftpCount != 1)
System.out.println(">> Found " + ftpCount + " instances of ftp");
if(ftpCount == 1)
System.out.println(">> Found " + httpCount + " instance of http");
if(httpsCount != 1)
System.out.println(">> Found " + httpsCount + " instances of https");
if(httpsCount == 1)
System.out.println(">> Found " + httpCount + " instance of http");
if(schemeOtherCount != 1)
System.out.println(">> Found " + schemeOtherCount + " instances of other schemes");
if(schemeOtherCount == 1)
System.out.println(">> Found " + schemeOtherCount + " instance of other schemes");
if(eduCount != 1)
System.out.println(">> Found " + eduCount + " instances of edu");
if(eduCount == 1)
System.out.println(">> Found " + eduCount + " instance of edu");
if(orgCount != 1)
System.out.println(">> Found " + orgCount + " instances of org");
if(orgCount == 1)
System.out.println(">> Found " + orgCount + " instance of org");
if(comCount != 1)
System.out.println(">> Found " + comCount + " instances of com");
if(comCount == 1)
System.out.println(">> Found " + comCount + " instance of com");
if(domainOtherCount != 1)
System.out.println(">> Found " + domainOtherCount + " instances of other domains");
if(domainOtherCount == 1)
System.out.println(">> Found " + domainOtherCount + " instance of other domains");
}
}
import java.io.*;
导入java.util.*;
导入java.io.*;
导入java.util.Scanner;
导入java.io.File;
导入java.net.URL;
导入java.net.MalformedURLException;
公开课P1{
公共静态void main(字符串[]args)引发FileNotFoundException,MalformedUrlexException{
int-httpCount=0;
int HttpScont=0;
int-ftpCount=0;
int schemeOtherCount=0;
int-eduCount=0;
整数计数=0;
int comCount=0;
int domainOtherCount=0;
int lineCount=0;
int fileLineCount=0;
int totalinecount=0;
字符串fileName=“”;
外部循环:如果(args.length>0){
对于(int i=0;i>从“+fileName”中获得“+fileLineCount+”行);
如果(fileLineCount!=1)
System.out.println(“>>从“+fileName”中获得“+fileLineCount+”行);
}
}
}
否则{
Scanner scanTwo=新扫描仪(System.in);
这里:while(scanTwo.hasNextLine()){
字符串scannedUrlTwo=scanTwo.nextLine();
if(scannedUrlTwo.equals(“end”)){
在这里休息;
}
URL twoURL=新URL(scannedUrlTwo);
lineCount++;
String scheme=twoURL.getProtocol();
String nearlyDomain=twoURL.getHost();
int twoLastPeriod=nearlyDomain.lastIndexOf(“.”);
int twoned=nearlyDomain.length();
String domain=nearlyDomain.substring((twoLastPeriod+1),twoEnd);
if(scheme.equals(“http”))
httpCount++;
if(scheme.equals(“https”))
HttpScont++;
if(scheme.equals(“ftp”))
ftpCount++;
如果(!scheme.equals(“http”)&&!scheme.equals(“https”)&&!scheme.equals(“ftp”))
schemeOtherCount++;
if(域等于(“edu”))
eduCount++;
if(domain.equals(“org”))
orgCount++;
if(domain.equals(“com”))
comCount++;
如果(!domain.equals(“edu”)&&&!domain.equals(“org”)&&&!domain.equals(“com”))
domainOtherCount++;
如果(行数==1)
System.out.println(“>>从标准输入得到1行”);
如果(行数!=1)
System.out.println(“>>从标准输入中获得“+lineCount+”行”);
}
}
如果(httpCount!=1)
System.out.println(“>>找到“+httpCount+”http实例”);
如果(httpCount==1)
System.out.println(“>>找到“+httpCount+”http实例”);
如果(ftpCount!=1)
System.out.println(“>>找到“+ftpCount+”ftp实例”);
如果(ftpCount==1)
System.out.println(“>>找到“+httpCount+”http实例”);
如果(HttpScont!=1)
System.out.println(“>>找到”+HttpScont+“https实例”);
如果(HttpScont==1)
System.out.println(“>>找到“+httpCount+”http实例”);
if(schemeOtherCount!=1)
System.out.println(“>>找到”+schemeOtherCount+“其他方案的实例”);
if(schemeOtherCount==1)
System.out.println(“>>找到”+schemeOtherCount+“其他方案的实例”);
如果(eduCount!=1)
System.out.println(“>>找到“+eduCount+”个edu实例”);
如果(eduCount==1)
System.out.println(“>>找到”+eduCount+“edu实例”);
如果(orgCount!=1)
System.out.println(“>>找到“+orgCount+”组织实例”);
如果(orgCount==1)