java - Writing a "\t" to a text file creates a line? -
i'm writing 2 textfiles , wanted eaiser way read textfiles without words , number laying close eachother chose write them tab seperate them. when class linenumberreader
seems think tab new line.
i have 2 classes. textwriting
, comparetextfiles
when run textwriting
class can output this:
(1) word1
(2) word2
(3) word3
so working intended. (i used spaces formatting purposes, file contains tabs correctly)
and in other class comparetextfiles
i compare 2 textfiles wrote first class. important code this.
string word,word2; int i; lnr = new linenumberreader(new filereader(file)); while (sc.hasnext() && sc2.hasnext()) { word = sc.next(); word2 = sc2.next(); lnr.readline(); if (word.equalsignorecase(word2)) { = lnr.getlinenumber(); system.out.println("line number: " + + " , word: [" + word + "]" + " , word " + "[" + word2 + "]" + " same!"); } else system.out.println("[" + word + "]" + " , " + "[" + word2 + "]" + " not same"); }
the output recieving is:
line number: 1 , word: [(1)] , word [(1)] same!
line number: 2 , word: [asd] , word [asd] same!
line number: 3 , word: [(2)] , word [(2)] same!
line number: 3 , word: [yeye] , word [yeye] same!
line number: 3 , word: [(3)] , word [(3)] same!
line number: 3 , word: [he] , word [he] same!
why stuck on 3 many times, , tab create new line of sort?
your code calls linenumberreader.readline
every scanner token. presuming a) each scanner uses default delimiter (in case each line has 2 tokens) b) linenumberreader.readline
increments value returned linenumberreader.getlinenumber
until the file has been read - each token (rather each line) increment until 3 lines read (and stop incrementing), resulting in output get.
an alternative suggestion (there many ways skin cat): consider using 2 scanner's read files, reading files using scanner.readline
method. each line, increment variable representing line number , parse lines needed.
int linecount = 0; while ( sc.hasnextline() && sc2.hasnextline() ){ linecount++; string line1 = sc.nextline(); string line2 = sc2.nextline(); //parse lines string[] line1tabs = line1.split("\t"); //etc... }
Comments
Post a Comment