0

だから私はこれに永遠に立ち往生しています。コマンドラインから複数のファイルを受け入れ、文字列内の特定の情報を行ごとに読み取るコードが必要です。コマンドラインにファイルがない場合は、スキャナーを介して標準を読み取る必要があります。ここが私がいるところです。

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   ");
    }
}
}
4

2 に答える 2

2

単一のスキャナーを使用する必要があります。

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);
}
于 2013-09-19T01:18:29.960 に答える