所以我一直坚持这个。我需要我的代码通过命令行接受多个文件,并通过行读取字符串中的特定信息。如果命令行中没有文件,则必须通过扫描程序读取标准。这就是我在的地方。
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 ");
}
}
}
答案 0 :(得分: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);
}
答案 1 :(得分:0)
如果您想从文件中读取,最常见的方法如下:
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
}