Java 从标准输入和/或命令行读取时遇到问题

Java 从标准输入和/或命令行读取时遇到问题,java,Java,所以我一直被困在这个问题上。我需要我的代码通过命令行接受多个文件,并通读字符串中特定信息的行。如果命令行中没有文件,则必须通过扫描仪读取标准。这就是我的目的 import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.*; public class P1 { static String readLine; static int lineCount

所以我一直被困在这个问题上。我需要我的代码通过命令行接受多个文件,并通读字符串中特定信息的行。如果命令行中没有文件,则必须通过扫描仪读取标准。这就是我的目的

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

public class P1 {

static String readLine;
static int lineCount;
static int httpCount;
static int httpsCount;
static int ftpCount;
static int otherSchemesCount;
static int eduCount;
static int orgCount;
static int comCount;
static int otherDomainsCount;
static String protocol;
String schemeString;
static String testedLine;

P1() {

    lineCount = 0;
    httpCount = 0;
    httpsCount = 0;
    ftpCount = 0;
    otherSchemesCount = 0;
    eduCount = 0;
    orgCount = 0;
    comCount = 0;
    otherDomainsCount = 0;
}



boolean testLine(String testedLine) {
    if (testedLine.equals("end")) {
        return true;
    } else {
        lineCount++;
        String[] schemePart = readLine.split(":");
        String part1 = schemePart[0];

        if (part1.equals("http")) {
            httpCount++;

        } else if (part1.equals("https")) {
            httpsCount++;

        } else if (part1.equals("ftp")) {
            ftpCount++;

        } else {
            otherSchemesCount++;

        }

        if (readLine.contains("edu")) {
            eduCount++;
        } else if (readLine.contains("org")) {
            orgCount++;
        } else if (readLine.contains("com")) {
            comCount++;
        } else {
            otherDomainsCount++;
        }

    }
    return false;
}

public static void main(String[] args) throws FileNotFoundException, IOException {

    int i = 0;

    File testFile = new File(args[i]);

    if (args[0] == null) {
        Scanner scan = new Scanner(System.in);
        readLine = scan.nextLine();
    } else {      
        Scanner scanFile = new Scanner(testFile);
        readLine = scanFile.nextLine();
    }

    P1 countLines = new P1();

    boolean isEnded = false;
    System.out.println("Enter a line of text. "
            + "\nType 'end' to stop and count previous lines.");
    while (!isEnded) {
        //String testLine = countLines.getLine();
        isEnded = countLines.testLine(testedLine);
    }
    if (lineCount == 1) {
        System.out.println(">> Got " + lineCount + " line");
    } else {
        System.out.println(">> Got " + lineCount + " lines");
    }
    if (httpCount == 1) {
        System.out.println(">> Found " + httpCount + " instance of http");
    } else {
        System.out.println(">> Found " + httpCount + " instances of http");
    }
    if (httpsCount == 1) {
        System.out.println(">> Found " + httpsCount + " instance of https");
    } else {
        System.out.println(">> Found " + httpsCount + " instances of https");
    }
    if (ftpCount == 1) {
        System.out.println(">> Found " + ftpCount + " instance of ftp");
    } else {
        System.out.println(">> Found " + ftpCount + " instances of ftp");
    }
    if (otherSchemesCount == 1) {
        System.out.println(">> Found " + otherSchemesCount + " instance of other schemes");
    } else {
        System.out.println(">> Found " + otherSchemesCount + " instances of other schemes");
    }
    if (eduCount == 1) {
        System.out.println(">> Found " + eduCount + " instance of edu");
    } else {
        System.out.println(">> Found " + eduCount + " instances of edu");
    }
    if (orgCount == 1) {
        System.out.println(">> Found " + orgCount + " instance of org");
    } else {
        System.out.println(">> Found " + orgCount + " instances of org");
    }
    if (comCount == 1) {
        System.out.println(">> Found " + comCount + " instance of com");
    } else {
        System.out.println(">> Found " + comCount + " instances of com");
    }
    if (otherDomainsCount == 1) {
        System.out.println(">> Found " + otherDomainsCount + " instance of other domains");
    } else {
        System.out.println(">> Found " + otherDomainsCount + " instances of other domains   ");
    }
}
}

您需要使用单个扫描仪:

Scanner scan = null;
if (args[0] == null) {
    scan = new Scanner(System.in);
} else {      
    scan = new Scanner(testFile);
}
然后在while循环中:

while (!isEnded) {
    readLine = countLines.getLine();
    if(readLine == null)
        break;
    isEnded = countLines.testLine(readLine);
}

如果要从文件中读取,最常见的方法是:

BufferedReader reader=new BufferedReader(new FileReader(new File(filename)));
我建议首先不要使用这么多静态变量。首先尝试设计代码,并制作一个伪代码,在没有太多细节的情况下找出代码应该是什么样子。例如:

String arg=args[0];
if(arg==null) //read from system.in{
Scanner =new Scanner(System.in);
String line=sc.nextLine()
while(line!=null && line.size()>2){
    updateCounters(line); 
    line=sc.nextLine()
}\\end while

}
else
{ 
  BufferedReader reader=new BufferedReader(new FileReader(new File(filename)));
  String line=reader.readline();
  //the rest are same as above
}

你知道你只读了一行,对吗?那么,你的问题是什么?更加具体,并提供sscce。你发布的代码太多了,我不会看的。我想,你至少需要添加一个
readOneFile(Scanner&file)
和一个
printResults()
函数,以简化你的程序,更容易实现你想做的事情。Jon Lin我感谢你这么快的响应。我最终得到了一个nullpointerexception。这是我在将它提交到我们的评分系统时的一个错误。当在命令行上列出文件并输入一行文本时,使用名为10urls.lis(5%的点)的输入文件测试程序失败。键入“end”停止并计数前几行。P1.main(P1.java:91)输出中P1.testLine(P1.java:37)处的线程“main”java.lang.NullPointerException中的异常不匹配;这个测试没有分数,知道是什么原因吗?@Geoff然后检查它是否为null并停止循环。我只是给你指出了正确的方向,我不会为你做你的工作,我很感激。我不想让任何人替我做事。我刚到了需要朝正确的方向轻推的地步,让它工作起来。谢谢你的帮助!我不知何故得到了如此多的静态变量,因为我不断地得到这个错误。“无法从静态上下文引用非静态变量lineCount”。不知道为什么。有什么想法吗?我以前从未遇到过那个问题。谢谢您的帮助。@Geoff这是因为您在
main()
方法中执行所有操作,该方法是静态的,您可以从静态方法调用access实例变量。你可以从一个非静态的方法访问它们,这很有意义。非常感谢。