If it's known that every input is a single digit (and there are no spaces), then parsing the file is pretty simple:
while(has more lines){
nextLine = read line
for each char c in nextLine
parse character into digit or operation
}
In Java, you can do that with a Scanner
or a BufferedReader
. Scanner
has a Scanner.hasNextLine()
method. In the case of BufferedReader
, you would do something like:
final BufferedReader br = new BufferedReader(new FileReader(fileName));
while((nextLine = br.readLine()) != null){
...
}
(see BufferedReader.readLine()
)
There are several ways to parse the characters into your symbols. For getting the digit, first test to make sure it is a digit:
boolean isDigit(final char c){
return c >= '0' && c <= '9';
}
If it is a digit, then find its value:
int getDigit(final char digit){
return digit - '0';
}
Otherwise, if it's an operation, then you need to use a switch statement to figure out what operation it is:
int getOp(final char c){
switch(c){
case OP1_CHAR : return OP1_CODE;
case OP2_CHAR : return OP2_CODE;
...
default: throw new RuntimeException(c + " is an unknown operation");
}
}
Since an operation causes you to pop from your stack, you don't really need to this intermediate step of assigning on OP_CODE rather you can just switch on your OP_CHAR and then do the required operation...something like:
void doOp(final char op){
switch(c){
case OP1_CHAR : perform operation 1 which probably requires
popping from the stack, doing some operation,
and pushing back onto the stack
break; // or return;
case OP2_CHAR : perform operation 2
break;
...
default: throw new RuntimeException(op + " is an unknown operation");
}
}