Reputation: 220
I have a program with the purpose of analyzing a text file that the user selects via typing in the full path of the text file when prompted.
I have managed to get the scanner into multiple classes however it will not work for each method simultaneously. For example I have a class that will print the amount of numbers in the file and another that will print the number of words in the file. However only the first method that is run will work, the other will display 0 of whatever the class is searching for(numbers, lines, words etc) even if the true value is not actually 0.
I'm really stuck with why this is happening, I have attached the main class with two other classes to show a clear example:
Main Class:
package cw;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.io.IOException;
public class TextAnalyser {
public static Scanner reader;
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out.println("Enter a filename");
String filename = in.nextLine();
File InputFile = new File (filename);
reader = new Scanner (InputFile);
LineCounter Lineobject = new LineCounter();
WordCounter Wordobject = new WordCounter();
Lineobject.TotalLines();
Wordobject.TotalWords();
}
}
Class that counts lines:
package cw;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.io.IOException;
public class LineCounter {
public static void TotalLines() throws IOException {
// Scanner sc = new Scanner(TextAnalyser.class.getResourceAsStream("test.txt"));
Scanner sc = TextAnalyser.reader;
PrintWriter out = new PrintWriter(new FileWriter("C:\\Users\\Sam\\Desktop\\Report.txt", true));
int linetotal = 0;
while (sc.hasNextLine()) {
sc.nextLine();
linetotal++;
}
out.println("The total number of lines in the file = " + linetotal);
out.flush();
out.close();
System.out.println("The total number of lines in the file = " + linetotal);
}
}
Class that Counts words:
package cw;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.io.IOException;
public class WordCounter {
public static Scanner sc = TextAnalyser.reader;
public static void TotalWords() throws IOException{
//Scanner sc = new Scanner(TextAnalyser.class.getResourceAsStream("test.txt"));
PrintWriter out = new PrintWriter(new FileWriter("C:\\Users\\Sam\\Desktop\\Report.txt", true));
int wordtotal = 0;
while (sc.hasNext()){
sc.next();
wordtotal++;
}
out.println("The total number of words in the file = " + wordtotal);
out.flush();
out.close();
System.out.println("The total number of words in the file = " + wordtotal);
}
}
For some reason only one will work at a time, one will always say there are 0, if someone could explain to me why this is happening and how to solve it, it would really help, Thanks!
Upvotes: 0
Views: 709
Reputation: 471
reader = new Scanner (InputFile);
contains a reference to the Scanner object, when you use public static Scanner sc = TextAnalyser.reader;
in both methods you are copying the reference of reader
into sc
, so all of them are the same object. Why having 2 variables referencing all the same object one of which is redefined two times with the same value?
The problem here is that the scanner reaches the end of the file and when you call it again (it is the same object) it has nothing more to read, so you should create another scanner object (maybe it is what you was attempting to do?). A better solution would be to read the file once and store the contents in some data structure.
Upvotes: 1