首先,让我作为序言:我正在编写一个程序,允许我拿三个文件,每个文件包含一段不同的圣经段落,并计算单词,行和字符的数量。我的一个问题是,文件的第一行仅包含特定翻译的版本(即KJV)。我想让它开始运行,同时跳过文件的第一行。
到目前为止,我的代码是:
import java.util.*;
import java.io.*;
public class Assign6
{
public static void main(String[]args) throws Exception{
int wordCount = 0;
int lineCount = 0;
int charCount = 0;
java.io.File file1 = new java.io.File("translation1.txt");
java.io.File file2 = new java.io.File("translation2.txt");
java.io.File file3 = new java.io.File("translation3.txt");
java.io.PrintWriter file4 = new java.io.PrintWriter("CompareInfo.txt");
Scanner input = new Scanner(file1);
Scanner input2 = new Scanner(file2);
Scanner input3 = new Scanner(file3);
while(input.hasNextLine()){
String line = input.nextLine();
lineCount++;
String str[] = line.split((" "));
for(int i = 0; i <str.length; i++){
wordCount++;
}
charCount += (line.length());
}
file4.println("In the King James, there are " + lineCount + " Lines, " + wordCount + " Words, and " + charCount + " Characters.");
lineCount = 0;
charCount = 0;
wordCount = 0;
while(input2.hasNextLine()){
String line = input2.nextLine();
lineCount++;
String str[] = line.split((" "));
for(int i = 0; i <str.length; i++){
wordCount++;
}
charCount += (line.length());
}
file4.println("In the NIV, there are " + lineCount + " Lines, " + wordCount + " Words, and " + charCount + " Characters.");
lineCount = 0;
wordCount = 0;
charCount = 0;
while(input3.hasNextLine()){
String line = input3.nextLine();
lineCount++;
String str[] = line.split((" "));
for(int i = 0; i <str.length; i++){
wordCount++;
}
charCount += (line.length());
}
file4.println("In the Message, there are " + lineCount + " Lines, " + wordCount + " Words, and " + charCount + " Characters.");
file4.close();
System.out.println("File written.");
}
}
如果它只是文本文件的第一行,那么你认为if语句只会增加一次吗?如果是这样的话,我究竟会怎么做呢?
答案 0 :(得分:0)
在循环通过其他行之前读取一行?喜欢:
Scanner input = new Scanner(file1);
Scanner input2 = new Scanner(file2);
Scanner input3 = new Scanner(file3);
input.nextLine();
input2.nextLine();
input3.nextLine();
while(input.hasNextLine()){
//etc
所以它只是跳过它们,因为读者已经读了一次。