• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compiler class to read file and parse in Java

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
input length
input width
recall length
recall width
dup2
add
push 2
mult
store perimeter
mult
pop
recall perimeter
end
 
Saloon Keeper
Posts: 15527
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sherry, welcome to CodeRanch!

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.

  • Here's some cleaned up code:
     
    Slynn Garrett
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15527
    364
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Why? What happened? Show us the code including where you think the call to translateName() should be, and then tell us what isn't working.
     
    Slynn Garrett
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks everyone I got it working wonderfully now.
     
    Marshal
    Posts: 79239
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15527
    364
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'm assuming ArrayDeque is used because the Queue interface is required. After all, you add instructions to the end of a pipeline, and you remove them from the front.
     
    Campbell Ritchie
    Marshal
    Posts: 79239
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That sounds right; I did see the word Queue in earlier posts. ArrayDeque is the fastest‑performing queue implementation I know about. It can also be used as a stack.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic