0

For example, if the expression can have + - / * as operators, and floating point numbers

and the input is "3.5+6.5-4.3*4/5"

How should write a program that returns "3.5" "+" "6.5" "-" "4.3" "*" "4" "/" "5" one by one, I tried to write regular expression for these i.e. "[*]", "[/]","[+]","[-]" and pattern for number, and use:

Scanner sc = new Scanner("3.5+6.5-4.3*4/5"); 

and use sc.hasNext(pattern); to loop through each pattern, I expect it to keep matching the start of the string until it match the number pattern, and sc.nect(pattern) to get "3.5", and then "+" and so on.

But It seems like scanner does not work like this, and after looking through some other classes, I could not find a good solution.

1
  • Scanner is generally useful only if you know the format of the input in advance. Your best approach is to read the whole line, then split it into operators and operands. Commented Dec 9, 2013 at 23:54

2 Answers 2

4

You can create two capture groups with the following expression:

Regex

([0-9\.]+)|([+*\/\-])

Brief explanation

This alternates between:

[0-9\.]+ which matches the floating point numbers

and

[+*\/\-] which matches the mathematical symbols.

Example Java code

List<String> matchList = new ArrayList<String>(); try { Pattern regex = Pattern.compile("([0-9.]+)|([+*/\\-])"); Matcher regexMatcher = regex.matcher(subjectString); while (regexMatcher.find()) { matchList.add(regexMatcher.group()); } } catch (PatternSyntaxException ex) { // Syntax error in the regular expression } 
Sign up to request clarification or add additional context in comments.

Comments

1

For non-trivial parsing, I advise use of a parser creator (such as JavaCC).

Alternatively, the example you listed might be covered using one of the many scripting languages supported by Java (including JavaScript). See here.

 ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); // Result is object of type "Double" Object result = engine.eval("3.5+6.5-4.3*4/5"); // Format output result System.out.println(new DecimalFormat("0.######").format(result)); 

... output of this code is

6.56

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.