-2

I wrote a simple calculation program. I want users to enter their request as 12+12 and return the answer. I used StringTokenizer, but I got an error and it doesn't show me any result. There was a mention that Calc is a superclass and MinusCalc and PlusCalc are subclasses. Does anyone have any idea?

    void inputLineData() { // This is just the function that use for this case                                

    System.out.println(" Plz enter your all numbers");
    String strAll = key.next();

    StringTokenizer st = new StringTokenizer(strAll);
    int n1 = Integer.parseInt(st.nextToken());
    String str = st.nextToken();
    int n2 =  Integer.parseInt(st.nextToken());




    switch (str.charAt(0)) {
        case '+':
            PlusCalc P = new PlusCalc(n1, n2);
            listCalc[indexCalc] = P;
            indexCalc++;

            break;
        case '-':
            MinusCalc M = new MinusCalc(n1, n2);
            listCalc[indexCalc] = M;
            indexCalc++;

            break;

        default:
            System.out.println("Error!");

      }

  }

And this is MinusCalc class:

   public class MinusCalc extends Calc {

@Override
public int func(){

    return n1 - n2 ; 
}

public MinusCalc(int n1, int n2) {
    super(n1, n2); 
  }



 }

And this is PlusCalc class:

   public class PlusCalc extends Calc {

 @Override
public int func(){

    return n1 + n2;
}

public PlusCalc(int n1, int n2) {
    super(n1, n2);
  }

}

And this is Calc class:

    public abstract class Calc {

   public Calc(int n1, int n2) { // constructor with parameters!!
    this.n1 = n1;
    this.n2 = n2;
}

int n1,n2;
public abstract int func();

}
J. Schei
  • 327
  • 3
  • 15
MarAnd
  • 11
  • 6
  • please share the whole stack trace, along with what line caused the error (mentioned in stacktraces) – user31415 Jul 05 '16 at 17:39
  • 1
    Based on the information that you have included I conclude that the error lies in the blueberry sandwich compositing. Fix that and all will be well. – DwB Jul 05 '16 at 17:40
  • The name of the exception would be nice but my wild guest would be Integer.parseInt() parsing signs or whitespace characters. – tirpitz.verus Jul 05 '16 at 17:42
  • From JavaDoc: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead. (https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html) – Unknown Jul 05 '16 at 17:43
  • @tirpitz.verus Here are the exception : Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java:349) at filecalculation.FileCalculation.inputLineData(FileCalculation.java:121) at filecalculation.FileCalculation.menu(FileCalculation.java:62) at filecalculation.FileCalculation.main(FileCalculation.java:24) Java Result: 1 – MarAnd Jul 05 '16 at 17:48
  • https://stackoverflow.com/help/mcve --- your code does not even compile. What is `key`? – Robert Jul 05 '16 at 17:55
  • @Robert because this code also does something else , I just put the function related to StringTokenizer here. – MarAnd Jul 05 '16 at 18:11

1 Answers1

0

Assuming your input is correct the following might help you (if you have more operators just append to the list). The true in the parameter means you want to use the given operator as a delimiter as well as an operator, meaning it will be returned as a token too.

StringTokenizer st = new StringTokenizer(strAll, "+-*/", true);
if (st.countTokens() == 3) {
    int operand1 = Integer.parseInt(st.nextToken().trim());
    String operator = st.nextToken();
    int operand2 = Integer.parseInt(st.nextToken().trim());

    switch (operator.charAt(0)) {
        case '+':
            PlusCalc P = new PlusCalc(operand1, operand2);
            listCalc[indexCalc] = P;
            indexCalc++;

            break;
        case '-':
            MinusCalc M = new MinusCalc(operand1, operand2);
            listCalc[indexCalc] = M;
            indexCalc++;

            break;

        default:
            System.out.println("Error!");

    }
}

NOTE: try to use another option mentioned in the comments instead of StringTokenizer if you have not to.

ujulu
  • 3,289
  • 2
  • 11
  • 14
  • Thanks for your correct answer!! but can you tell me what trim() does in this line ? int operand1 = Integer.parseInt(st.nextToken().trim()); – MarAnd Jul 06 '16 at 12:40
  • You're welcome. `trim()` removes spaces around the tokens; otherwise a number with spaces before and after it will not be a valid number und cannot be parsed. That means `12 + 12` and `12+12` and `12 + ` and so on are treated the same way. – ujulu Jul 06 '16 at 13:02