Hi, this is a little stack machine program I wrote.
The only thing I can't get to work is if there is no 'HLT' command at the end of the data file supplied to the program, it's supposed to print out "[warning - no HLT instruction]". The problem is, it ALWAYS prints that out because I can't get my executeCurrentInstruction to change the boolean variable in main from false to true. This also causes problems with the while loop in my main().
TL;DR: How change boolean in main() from false to true from within a sub method in the same class?
Thank you! And any criticism on my code is welcome, I'm new at this
EDIT: I'm not sure, but it might be helpful if I actually included my code.
Code:
The only thing I can't get to work is if there is no 'HLT' command at the end of the data file supplied to the program, it's supposed to print out "[warning - no HLT instruction]". The problem is, it ALWAYS prints that out because I can't get my executeCurrentInstruction to change the boolean variable in main from false to true. This also causes problems with the while loop in my main().
TL;DR: How change boolean in main() from false to true from within a sub method in the same class?
Thank you! And any criticism on my code is welcome, I'm new at this

EDIT: I'm not sure, but it might be helpful if I actually included my code.
Code:
Code:
import java.io.*;
import java.lang.*;
import java.util.*;
public class Proj36527 {
public static void main(String[] args) {
boolean halt = false;
if (args.length < 1) {
System.out.println("Error: Must supply an argument.");
System.exit(1);
}
File instructionsFile = new File(args[0]);
//Header output
System.out.println("Expression Stack Machine - A. McKibben");
System.out.println("File: " + instructionsFile);
System.out.println();
//Creates a new stack
Stack<Integer> stackMachine = new Stack<Integer>();
//Attempts to read and execute instructions from file supplied at command line
try{
Scanner instructionScan = new Scanner(instructionsFile);
while (instructionScan.hasNextLine() && (halt != true)) {
String [] currentLine = new String[0];
currentLine = splitCurrentLine(instructionScan, currentLine);
String currentInstruction = new String();
currentInstruction = getThisInstruction(instructionScan, currentLine, currentInstruction);
executeCurrentInstruction(currentInstruction, halt, stackMachine, instructionScan, currentLine);
}
} catch (Exception e) {
e.printStackTrace();
}
if (halt == false) {
System.out.println("[warning - no HLT instruction]");
}
}
//Splits the current line
private static String[] splitCurrentLine(Scanner instructionScan, String[] currentLine) {
currentLine = instructionScan.nextLine().toUpperCase().split(" ");
return currentLine;
}
//Returns the current instruction
private static String getThisInstruction(Scanner instructionScan, String[] currentLine, String currentInstruction) {
currentInstruction = currentLine[0];
return currentInstruction;
}
//Executes current Instruction
private static void executeCurrentInstruction(String currentInstruction, boolean halt, Stack<Integer> stackMachine, Scanner instructionScan, String[] currentLine) {
int topOfStack = 0;
int nextToTop = 0;
switch (currentInstruction) {
case "CLR": stackMachine.clear();
break;
case "HLT": halt = true;
System.out.println("[machine halted]");
//System.exit(1);
break;
case "NEG": topOfStack = stackMachine.pop();
topOfStack = (topOfStack * -1);
stackMachine.push(topOfStack);
break;
case "ADD": topOfStack = stackMachine.pop();
nextToTop = stackMachine.pop();
int sum = (topOfStack + nextToTop);
stackMachine.push(sum);
break;
case "SUB": topOfStack = stackMachine.pop();
nextToTop = stackMachine.pop();
int difference = (nextToTop - topOfStack);
stackMachine.push(difference);
break;
case "MUL": topOfStack = stackMachine.pop();
nextToTop = stackMachine.pop();
int product = (nextToTop * topOfStack);
stackMachine.push(product);
break;
case "DIV": topOfStack = stackMachine.pop();
nextToTop = stackMachine.pop();
if (topOfStack == 0) {
System.out.println("error - division by zero");
System.exit(1);
}
int quotient = (nextToTop/topOfStack);
stackMachine.push(quotient);
break;
case "LDC": int constantToLoad = 0;
try {
constantToLoad = Integer.parseInt(currentLine[1]);
} catch (NumberFormatException n) {
System.out.println("[warning - illegal command '" + currentLine[0] + " " + currentLine[1] + "' ignored]");
}
stackMachine.push(constantToLoad);
break;
case "INP": System.out.print("input: ");
Scanner kbd = new Scanner(System.in);
int kbdInput = 0;
try {
kbdInput = kbd.nextInt();
} catch (InputMismatchException i) {
System.out.println("[warning - illegal input ignored]");
}
stackMachine.push(kbdInput);
break;
case "OUT": topOfStack = stackMachine.pop();
System.out.println("output: " + topOfStack);
System.out.println();
break;
case "NLN": System.out.println();
break;
case "DBG": System.out.println("DEBUG: " + stackMachine + " (top)");
break;
default: System.out.println("[warning - no legal commands]");
break;
}
}
}