0

这是针对学校作业的,我必须接受一个命令行参数,该参数包含包含 URL 的文件。如果没有命令行参数,我必须从标准输入中获取一个参数。然后,我必须报告这些 URL 中存在多少种特定的域和方案,并报告在每个文件中读取了多少行。程序应该读取行,直到它到达“结束”。我没有得到我应该得到的正确输出。它应该输出正确的显示次数的计数。例如,如果您通过命令行输入包含“ https://www.facebook.com/ftp://ftp.bls.gov/pub/special.requests/cpi/cpiai.txt end ”的列表,它应该报告回来

">>找到一个 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");
  }
}
4

0 回答 0