class - Scanner only works with one out of multiple classes Java -
i have program purpose of analyzing text file user selects via typing in full path of text file when prompted.
i have managed scanner multiple classes not work each method simultaneously. example have class print amount of numbers in file , print number of words in file. first method run work, other display 0 of whatever class searching for(numbers, lines, words etc) if true value not 0.
i'm stuck why happening, have attached main class 2 other classes show 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 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 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 file = " + linetotal); out.flush(); out.close(); system.out.println("the total number of lines in file = " + linetotal); } }
class 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 file = " + wordtotal); out.flush(); out.close(); system.out.println("the total number of words in file = " + wordtotal); } }
for reason 1 work @ time, 1 there 0, if explain me why happening , how solve it, help, thanks!
reader = new scanner (inputfile);
contains reference scanner object, when use public static scanner sc = textanalyser.reader;
in both methods copying reference of reader
sc
, of them same object. why having 2 variables referencing same object 1 of redefined 2 times same value?
the problem here scanner reaches end of file , when call again (it same object) has nothing more read, should create scanner object (maybe attempting do?). better solution read file once , store contents in data structure.
Comments
Post a Comment