-3
public static void clacMethod() {
    int result = 0;
    // int i;
    System.out.println("Enter numbers to calculation");
    String input = new Scanner(System.in).nextLine();
    String[] inputSplit = new String[input.length()];
    int[] output = new int[inputSplit.length];
    for (int i = 0; i < inputSplit.length; i++) {
        if (input.contains("-")) {
            inputSplit = input.split("\\-");
            output[i] = Integer.parseInt(inputSplit[i]);
            result = output[0];
            result -= output[i];
        }
        if (input.contains("+")) {
            inputSplit = input.split("\\+");
            output[i] = Integer.parseInt(inputSplit[i]);
            result = output[0];
            result = result + output[i];
        }
    }
    System.out.println(result);
}

how i have to work

How it have to work "+" and "-" operations on String input

// input should be like: 12+10-4 // output should be: 18

Hasan Hawar
  • 23
  • 1
  • 9
  • 2
    Check-out this post https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java to know how to split strings in Java. – Francois Nadeau Jan 22 '19 at 16:22
  • 1
    Possible duplicate of [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – SanSolo Jan 22 '19 at 16:28

1 Answers1

2

if is not a school exercise use ScripEnginer

public double calc(String input) {
    int result;
    ScriptEngineManager sem= new ScriptEngineManager();    
    ScriptEngine engine = sem.getEngineByName("JavaScript");     
    result = (Double)engine.eval(input)
    return result;
}

where input is a String "12+10-4" and result will be a double 8

imperezivan
  • 761
  • 5
  • 13