0

Good morning everyone, I'm a beginner student in java programming. So, direct to the point, I'm creating a program that can read and compile a .txt file with a click of a button.

I'm already done with the reading of the file. My problem is that my program doesn't compile the text from the first JTextArea and show the results to the second JTextArea.

I've been having this problem for four days now, I've been trying to change the code in any possible ways I can think of. Can someone enlighten me and tell me what's wrong with my code? It will surely help me a lot.

Many thanks to all.

    @SuppressWarnings("IncompatibleEquals")
private void executeCodeActionPerformed(java.awt.event.ActionEvent evt) {                                            
    String loadi = "Load";
    String add = "Add";
    String subt = "Subt";
    String mult = "Mult";
    String div = "Div";
    String input = "Input";
    String print = "Print";
    int number = 0;

    String txt = textAreaCode.getText();
    String split = " ";
    String [] text = txt.split(split);
    String word = text[0];
    int num = Integer.getInteger(text[1]);
    int result = num;
    int result1 = 0;
    Scanner scan = new Scanner(txt);
    scan.nextLine();

    for (int count=0;count<txt.length();count++ ) {
        if (loadi.equalsIgnoreCase(word)){
            result1 = num + number;
        }
        else if (add.equalsIgnoreCase(word)){
            result1 = num + result;
        }
        else if (subt.equalsIgnoreCase(word)){
            result1 = num - result;
        }
        else if (mult.equalsIgnoreCase(word)){
            result1 = num * result;

        }
        else if (div.equalsIgnoreCase(word)){
            result1 = num / result;

        }
        else if (print.equalsIgnoreCase(word)){
            textAreaOutput.setText(String.valueOf(result1));
        }
        else if (input.equalsIgnoreCase(word)){
            String nmbr = inputField.getText();
            int nmr = Integer.parseInt(nmbr);
            result1 = nmr + number;
        }
    }

1 Answers1

0

I see a few errors in your code that might add up to not working at all. Here they are:

  1. The variable word is never updated to the following tokens (it is set to text[0] at first then never changed). Same goes for num.

  2. All operations act on variables num and result, then put the result into result1. So intermediate results are not carried from an operation to the next.

  3. The "input" operation load current value from inputField, without waiting for the user to actually type something.

  4. The main loop iterate until count reach the number of characters in the program; it should loop for the number of tokens instead.

Also, here are a few suggestions to make your code more ligible:

  1. If you are beginning in Java, then get rid of the Scanner object, and keep only the "split on spaces" approach. I would not recommend this for real problems, but Scanner is somewhat complex to use correctly.

  2. Wrap the splited words array into a List collection, then obtain an iterator from it. Here it is:

    Iterator<String> words = Arrays.asList(txt.split("[ ]+")).iterator();

    Then, write your loop as:

    while (words.hasNext()) { String command = words.next(); ... }

  3. Move your operation mnemonics outside of the function, and mark them as final.

  4. Read your operation arguments from inside each operation block; this is required because some operation do not receive arguments.

Well, I won't give you the code, as this is something you really have to do by yourself. Hope that helps. Good luck.

James
  • 4,211
  • 1
  • 18
  • 34