java - How to move up lines in a .txt file with BufferedReader? -
making "assembler" program cs course enrolled in. has functions add, set, inc (increment), , jig. now, inputing .txt file following layout (as example):
keep note: , b integer's store value throughout program, , print out value once reaches end of text file.
inc (increments 1) set b 5 (set's b's value 5) inc b add 3 (add's 3 a's current value) jig b -4 (move's backward 4 lines, inc a)
so confused how move bufferedreader 4 lines? there method in bufferedreader lets move index/position? otherwise, how else can accomplish this?
the simplest thing store lines in array or list.
list<string> lines = files.readalllines(paths.get("myfile.txt"));
this allow progress line @ random.
to line can use lines.get(n)
example can do
int pointer = 0; for(boolean running = true; running && pointer < lines.size(); ) { string line = lines.get(pointer); string[] parts = line.split(" +"); switch(part[0]) { case "jmp": pointer += integer.parseint(parts[1]); // jump or forth. continue; case "halt": running = false; break; // other instructions } pointer++; }
Comments
Post a Comment