gpt4 book ai didi

java - 我在从标准输入和/或命令行读取时遇到问题

转载 作者:行者123 更新时间:2023-12-02 06:43:29 25 4
gpt4 key购买 nike

所以我一直坚持这个。我需要我的代码通过命令行接受多个文件,并读取各行以获取字符串中的特定信息。如果命令行中没有文件,则必须通过扫描仪读取标准。这就是我现在所在的位置。

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

关于java - 我在从标准输入和/或命令行读取时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18884760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com