2

I am trying to make a simple calculator application that would take a string like this

5 + 4 + 3 - 2 - 10 + 15

I need Java to parse this string into an array

{5, +4, +3, -2, -10, +15}

Assume the user may enter 0 or more spaces between each number and each operator

I'm new to Java so I'm not entirely sure how to accomplish this.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
maček
  • 76,434
  • 37
  • 167
  • 198
  • The easiest way to go about it would be with regular expressions (the `Matcher` and `Pattern` classes). That being said, are you sure that's really what you want to do? What would you do with those two arrays? – Brian Roach Nov 14 '11 at 01:29
  • I guess this falls outside the spec. of your homework, but the *easy* way to (parse &) evaluate the `String` is using the `ScriptEngine` as seen in [this example](http://stackoverflow.com/questions/7441625/how-to-find-a-button-source-in-awt-calculator-homework/7441804#7441804). – Andrew Thompson Nov 14 '11 at 02:05

6 Answers6

0

You can use Integer.parseInt to get the values, splitting the string you can achieve with String class. A regex could work, but I dont know how to do those :3

Zavior
  • 6,412
  • 2
  • 29
  • 38
0

Take a look at String.split():

String str = "1 + 2";
System.out.println(java.util.Arrays.toString(str.split(" ")));

[1, +, 2]

Note that split uses regular expressions, so you would have to quote the character to split by "." or similar characters with special meanings. Also, multiple spaces in a row will create empty strings in the parse array which you would need to skip.

This solves the simple example. For more rigorous parsing of true expressions you would want to create a grammar and use something like Antlr.

Scott A
  • 7,745
  • 3
  • 33
  • 46
0

Let str be your line buffer.

Use Regex.match for pattern ([-+]?[ \t]*[0-9]+).

Accumulate all matches into String[] tokens.

Then, for each token in tokens:

String s[] = tokens[i].split(" +");
if (s.length > 1)
    tokens[i] = s[0] + s[1];
else
    tokens[i] = s[0];
moshbear
  • 3,282
  • 1
  • 19
  • 33
0

You can use positive lookbehind:

    String s = "5  +  4  +  3   -   2 - 10 + 15";
    Pattern p = Pattern.compile("(?<=[0-9]) +");
    String[] result = p.split(s);

    for(String ss : result)
        System.out.println(ss.replaceAll(" ", ""));
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
0
    String cal = "5 + 4 + 3 - 2 - 10 + 15";
    //matches combinations of '+' or '-', whitespace, number
    Pattern pat = Pattern.compile("[-+]{1}\\s*\\d+");
    Matcher mat = pat.matcher(cal);
    List<String> ops = new ArrayList<String>();
    while(mat.find())
    {
        ops.add(mat.group());
    }
    //gets first number and puts in beginning of List
    ops.add(0, cal.substring(0, cal.indexOf(" ")));

    for(int i = 0; i < ops.size(); i++)
    {
        //remove whitespace
        ops.set(i, ops.get(i).replaceAll("\\s*", ""));
    }

    System.out.println(Arrays.toString(ops.toArray()));
    //[5, +4, +3, -2, -10, +15]
Nick Rolando
  • 25,879
  • 13
  • 79
  • 119
0

Based off the input of some of the answers here, I found this to be the best solution

// input
String s = "5 + 4 + 3 - 2 - 10 + 15";
ArrayList<Integer> numbers = new ArrayList<Integer>();

// remove whitespace
s = s.replaceAll("\\s+", "");

// parse string
Pattern pattern = Pattern.compile("[-]?\\d+");
Matcher matcher = pattern.matcher(s);

// add numbers to array
while (matcher.find()) {
  numbers.add(Integer.parseInt(matcher.group()));
}

// numbers
// {5, 4, 3, -2, -10, 15}
maček
  • 76,434
  • 37
  • 167
  • 198