io - Echo class in Java -
please me hw assignment. supposed modify echonumber class extends echo class count number of characters in every line in text file in addition displaying text. here echo class:
import java.util.scanner; import java.io.*; public class echo{ string filename; // external file name scanner scan; // scanner object reading external file public echo(string f) throws ioexception { filename = f; scan = new scanner(new filereader(filename)); } // reads lines, hands each processline public void readlines(){ while(scan.hasnext()){ processline(scan.nextline()); } scan.close(); } // real processing work public void processline(string line){ system.out.println(line); } }
here echonumber class, notice says "your code goes here":
import java.io.*; public class echonumber extends echo { // current line number private int linenumber; public echonumber (string datafile) throws ioexception { super( datafile); linenumber=1; } // prints line leading line number , trailing line length // overrides processline method in echo class public void processline(string line){ /* code goes here */ } }
here echotester class:
import java.io.*; import java.util.scanner; public class echotester { public static void main(string[] args) { // uses try/catch handle ioexceptions in main try { string filename; scanner namereader = new scanner(system.in); system.out.println("enter file name"); filename = namereader.nextline(); echonumber e = new echonumber(filename); e.readlines(); } catch(ioexception e) { e.printstacktrace(); } } }
and .txt file:
the best things in life free stitch in time saves 9 still waters run deep teaches ill teaches can not take when die better untaught ill taught not cross bridges before come them learnt forgotten worm turn last straw broke camels way mans heart through stomach if stone fall upon egg alas egg if egg fall upon stone alas egg there there way marry in haste , repent @ leisure 1 tongue enough woman if wish advice consult old man best advice found on pillow clouds bring not rain can not tell book cover no news news bad news travels fast live , let live birds of feather flock time men have time come aid of country in live
the output supposed like:
1 best things in life free-32
2 stitch in time saves nine-27
3 still waters run deep-21
4 teaches ill teaches all-30
5 can not take when die-41
6 better untaught ill taught-31
7 not cross bridges before come them-49
8 learnt forgotten-26
9 last straw broke camels back-48
except without spaces between each line. reason fuses 1 paragraph if did not separate each line.
something this:
public void processline(string line){ system.out.println(linenumber + " " + line + "-" + line.length()); ++linenumber; }
i haven't tested it, if isn't 100% correct, i'll leave exercise complete, should put on right track. luck.
Comments
Post a Comment