Making an "assembler" program for a CS course I am enrolled in. It has functions like ADD, SET, INC (increment), and JIG. Now, we are inputing a .txt file with the following layout (as example):
Keep note: A and B are just integer's that store the value throughout the program, and print out the value once it reaches the end of the text file.
INC A (increments A by 1)
SET B 5 (set's B's value to 5)
INC B
ADD A 3 (add's 3 to A's current value)
JIG B -4 (move's backward 4 lines, so back to INC A)
So what I am confused how to do is move my BufferedReader back 4 lines? Is there a method in BufferedReader that lets you move it to a certain index/position? Otherwise, how else can I accomplish this?
The simplest thing to do is store the lines in an array or List.
List<String> lines = Files.readAllLines(Paths.get("myfile.txt"));
This will allow you to progress to any line at random.
To get any line you can use lines.get(n)
For example you 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 back or forth.
continue;
case "HALT":
running = false;
break;
// other instructions
}
pointer++;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.