jwarner112
jwarner112

Reputation: 1502

Using Java split to evaluate a mathematical expression

After some time of working with this problem, I'm having trouble figuring out the Regex syntax for exactly what I need.

In a Java course I'm taking, we're given the task of building a calculator capable of taking an expression string (written with or without parentheses) and correctly evaluating it.

The concept behind the parsing of the expression is that you have a string...

5+16/20-56+7-20*16/9

And you want to get two ArrayLists via 'split':

{5,16,20,56,7,20,16,9} {+,/,-,+,-,*,/}

You then perform operations piece by piece, in order of operation priority, emptying operations from your ArrayList until your number array has one element: the final result.

Unfortunately, I have no experience with Regex and can't make any sense of the tutorials and examples I'm reading.

I can't figure out how to split based on "[*-/+]", and I can't figure out how to select whole numbers to split by rather than mere digits. I can't get my code to compile because of these errors. The one working snippet of code I did get:

import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Tester
{
    public static void main(String[] args)
    {
        String expression = "3+5+6-10*20/150-14+76%2";

//      String[] operands = expression.split("");
        String[] operators = expression.split("[0-9]");

//      System.out.println(operands);
        System.out.println(operators);
    }
}

results in

[Ljava.lang.String;@308ab721

And I have no idea where that comes from.

Can anyone help me figure out what I'm doing wrong? Or point to a very good beginner's lesson on Regex?

I appreciate your help, everyone.

Upvotes: 1

Views: 2526

Answers (4)

Bohemian
Bohemian

Reputation: 425378

If it's simply numbers or non numbers, the regex is simple:

String[] operands = expression.split("\\D+");
String[] operators = expression.replaceAll("^\\d+", "").split("\\d+");

The regex meanings are:

The call to replaceAll() trims leading digits from the expression, so you don't get a blank as the first operator.

Upvotes: 0

Azar
Azar

Reputation: 1106

I'm no expert, but this should work for you:

public static void main(String[] args)
{      
  String expression = "3+5+6-10*20/150-14+76%2";

  String[] operands = expression.split("[-+*%/]");
  String[] operators = expression.split("[0-9]+");

  System.out.println(Arrays.toString(operands));
  System.out.println(Arrays.toString(operators));
}

You need to escape Regex meta-characters (or in this case just change the order), and print your arrays properly.

A little elaboration:

In Regex, certain characters have special meaning. You've used these already by defining the range 0-9, with the dash being the meta-character. The same applies to -. You can see a complete list here.

I actually made use of the + meta-character (which "matches the preceding subexpression one or more times") in the modified number matching pattern to reduce the number of empty strings that split picked up. It does this by matching one or multiple occurrences of a digit.

Upvotes: 1

Baby
Baby

Reputation: 5102

operands and operators is an array

to print array you can do

System.out.println(Arrays.toString(operands));
System.out.println(Arrays.toString(operators));

And you should split it like this:

String[] operands = expression.split("[\\+\\-\\*\\%\\/]");
String[] operators = expression.split("[0-9]+");

Upvotes: 2

Kon
Kon

Reputation: 10810

You're printing out the memory address of your variables. If you want a string representation, you may do

System.out.println(Arrays.toString(operators));

What's happening behind the scenes is the println() method is calling toString() on any object that is passed to it. The default implementation of toString() for an array in Java to is return the memory address, which is why you get this ugly thing printing out.

Upvotes: 1

Related Questions