Someone please help! I am trying to read a text file from a Translator class in java then I have another class to read it as instructions to do. I have some code but when I test my compiler it brings me back an empty ArrayDeque. I have also forgotten how to call the methods from the other class for instructions after it has found instructions. I am not wanting someone to code this all for me but I need someone to explain where I am going wrong. Here are my codes so far.
I have not been able to figure out the parsing of this file.
My text file is in this format:
You never actually add anything to your deque? Shouldn't you call translateName() somewhere in your getInstructionList() method?
A few more remarks:
Use packages. Putting your classes in the default package will almost always lead to problems
Make classes final unless you have plans to extend them. You should almost never have to make your classes extensible.
Don't make classes and methods public unless you intend to expose your API. Classes only have to be public if you use them outside of their package.
If a method doesn't require access to instance fields, you can make it static.
If a class consists only of static methods, you can make it a utility class by providing only a private constructor with an empty implementation.
Don't use String where you can use strong types instead. Use Path to indicate file paths.
It looks like you have an unused field filename in your Translator class, that is being hidden by a parameter in getInstructionList(). Get rid of it.
Declaring your method return types and variable types as ArrayDeque is too strong. If you need a queue, use the Queue interface. If you need a stack, use the Deque interface.
Don't pass generic type arguments twice. You can use the diamond operator instead.
Avoid translating exceptions to return values. Just propagate them. If getInstructionList() doesn't have an appropriate way to deal with an IOException (after all, it shouldn't know whether the application has a GUI), just declare that it throws an IOException.
Use try-with-resources to read from files.
Swing code (like showing a message dialog) should be run on the EDT. You can do this using SwingUtilities.invokeLater().
Don't use integers for typed constants. Use enums instead. You can make OpCode an enum and associate Instructions with OpCodes.
I am not exactly sure why but my teacher insisted on using ArrayDeque. As for where to call my translateName I can not find anywhere it will work. I thought it would be in the while loop but doesn't help.
Sherry Kaiser wrote:I am not exactly sure why but my teacher insisted on using ArrayDeque. . . .
That does not mean you declare it as such
Queue<Foo> queue = new ArrayDeque<>(); If you are using array deque as a stack, then you would have to declare it as such. There is a serious problem about stacks in Java® because of the java.util.Stack class which is a triumph of bad design, and there is no stack interface. Have you read about Array Deque?