Jacob Porter Jacob Porter - 1 month ago 11
Java Question

Need to only read from the second line down

First off, let me preface: I'm writing a program that would allow me to take three files, each containing a different translation of a passage of the Bible, and calculating the number of words, lines, and characters. My one problem is, the first line of the file contains merely the version of the particular translation (i.e. KJV). I want to have it begin to run while also skipping over the first line of the file.

So far, what I have for my code is this:

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


Do you suppose an if statement would work where I merely increment it once if it is the first line of the text file and if so how exactly would I go about doing that?

Answer

Read one line once before looping through the other lines? Like:

Scanner input = new Scanner(file1);
Scanner input2 = new Scanner(file2);
Scanner input3 = new Scanner(file3);

input.hasNextLine();
input2.hasNextLine();
input3.hasNextLine();

while(input.hasNextLine()){
//etc

So it just skips them because the reader already read it once.

Comments