Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8277453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:41:46+00:00 2026-06-08T08:41:46+00:00

I have written a Java program that reads in a file containing commands to

  • 0

I have written a Java program that reads in a file containing commands to execute(in a language I made up myself). The commands are read in as strings and put into an array. Now a “dispatcher”-method will loop through the array, interpreting the commands and calling the respective methods which will act upon them.

This of course leads to a big block of nested if-statements:

if commandReadIn == this, do that... 
if commandReadIn is of type x, get next element,treat next element as argument... 
etc. 

Right now I only have a handful of commands, but what if I wanted to add hundreds? The code would become unmaintainable.

Now I’m wondering if it’s possible to completely get rid of the conditional logic. The command pattern doesn’t seem to be of much use here, since I would have to interpret the strings at some point anyway.. which means lots of nested “if”s. If it’s not possible, what would be the best approach of restructuring the commands and their grammar in a way that will make it easy to add, edit or remove commands?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:41:47+00:00Added an answer on June 8, 2026 at 8:41 am

    Use the Command Pattern for your commands. Your implementation can be greatly simplified.

    1) Create a Command interface with an execute method.
    2) Create an implementation for each command.
    3) When you start your program, create a map of command string -> command implementation.
    4) When you read in a string, look up the appropriate implementation and invoke it.
    5) Optionally, your execute method can take a custom Context object as an argument, allowing you to pass your command arguments in a generalized way. It’s up to the implementations to understand the context object and retrieve arguments off it.

    With this approach, you will have no if statements, except possibly a check to see if you failed to retrieve anything from your cache of command implementations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program written in Java that reads an excel file and output
I have a program written in Java that reads in a file that is
I have written a program in Java to read in a text file of
I have written a program that reads in a File object (really an XML
I have written a java program that is actually works as a gui to
I have a program which i have myself written in java, but I want
I have written a java program that tests the speed of a couple of
I have written a function in my program that allows me to retrieve strings
I have written a short Scala program to read a large file, process it
I have written a nice program in Java that connects to a gmail account

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.