0

I am working on my first Calculator project on GUI, I get the input and concate it in a String field by using Action Listner.

calculation = calculation + " 4 ";
calculation = calculation + " * ";
calculation = calculation + " 9 ";

Taking the first number and the second number by using substring, converting them and putting them in two fields

num1 = Integer.parseInt(calculation.substring(0, 1));
num2 = Integer.parseInt(calculation.substring(4, 5));

enter image description here

The problem is that I cannot use substring more than x digits before the operator and y digits after the operator. Can I do that by using subString built-in method? and If I can't, How can I do that by using any other methods?

enter image description here

1 Answers1

1

You could use String.split and String.contains:

    String equation = "4+ 10";
    equation.replaceAll(" ", "");//get rid if the space

    String[] buffer = null;
    if (equation.contains("+"))
        buffer = equation.split("\\+");
    else if (equation.contains("*"))
        buffer = equation.split("\\*");
    else if (equation.contains("/"))
        buffer = equation.split("\\/");
    else if (equation.contains("-"))
        buffer = equation.split("\\-");


    if(buffer != null && buffer.length == 2)
    {
        int term1 = Integer.parseInt(buffer[0]);
        int term2 = Integer.parseInt(buffer[1]);

        //call you calculation  ode

    }else
        System.out.println("Fail to parse equation");

Or use a single regex as Vince Emigh proposed:

    String equation = "4+ 10";
    equation.replaceAll(" ", "");//get rid if the space

    final String [] buffer = equation.split("\\+|\\*|\\-|\\/");


    if(buffer.length == 2)
    {
        int term1 = Integer.parseInt(buffer[0]);
        int term2 = Integer.parseInt(buffer[1]);

        //call you calculation  ode

    }else
        System.out.println("Fail to parse equation");
ortis
  • 2,203
  • 2
  • 15
  • 18
  • `String[] split = equation.split("\\+|\\*|\\-|\\\");` Try using regex. Also, I suggest replacing all spaces before splitting – Vince Oct 30 '14 at 17:10
  • 1
    @Vince Emigh your regex is better but you got the divide sign wrong. It should be `String[] split = problem.split("\\+|\\*|\\-|\\/")`. Space are not an issue, since `String.trim` is called before `Integer.parse` – ortis Oct 30 '14 at 17:13
  • No need for `trim()` if you `replaceAll()` at first. Keep in mind, when you call `trim()`, you are creating a new String. Same with `replaceAll()`, but you're only creating 1 new String with replaceAll, 2 with trim (since you're calling it twice). Always gotta keep that stuff in mind when working with Strings, they're immutable. And sorry about the divide, was typin fast. – Vince Oct 30 '14 at 17:15
  • Did not know about `String.trim` creating new `String`. Then I agree with you, better use `String.replaceAll`. – ortis Oct 30 '14 at 17:19
  • All methods that allow you to mutate the String in any way (even `toCharArray()`) will return a new String, since Strings are immutable. And although your current answer works, `contains` is bothering me more than it should. +1, but if you don't mind, please throw in the regex I mentioned; it'll look a lot cleaner, removing all that boilerplate code – Vince Oct 30 '14 at 17:26