所以我一直坚持这一点。我需要我的代码通过命令行接受多个文件,并通过这些行读取字符串中的特定信息。如果命令行中没有文件,则必须通过扫描仪读取标准。这就是我所在的地方。
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 ");
}
}
}