2
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class hw2 {
    
    static char[] evaluate(String input) throws IllegalArgumentException 
    {
        input = input.replaceAll("\\p{Z}", "");
        
        String result_num1 = null;
        String result_op = null;
        String result_num2 = null;
        
        Pattern pattern_num = Pattern.compile("([+-][0-9]*)([+-/*])([+-]\\d*)");
        Matcher matcher_num = pattern_num.matcher(input);
        
        while(matcher_num.find()) {
            result_num1 = matcher_num.group(1); 
            result_op = matcher_num.group(2); 
            result_num2 = matcher_num.group(3); 
        }
        
        System.out.println(result_num1);
        System.out.println(result_op);
        System.out.println(result_num2);
          
        return element_2; 
    }
  
    public static void main(String[] args) throws Exception{
        
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(isr);
        String input = reader.readLine();
        
        char[] result = evaluate(input); 
        System.out.println(result);
        
    }  
}

Could anyone please guide on this one.

input: 300 + 25

expected output:
300
+
25
+

output:
null
null
null

0

1 Answer 1

2

Based on your shown samples, could you please try following.

([+-]?\\d+)([-+/*])([+-]?\\d+)

Explanation: Adding detailed explanation for above.

([+-]?\\d+)  ##Creating first group which checks if there is either +/- present(as optional) following with 1 or more occurrences.
([-+/*])     ##Creating 2nd group and checking if +/- or / is present
([+-]?\\d+)  ##Creating 3rd group which checks if there is either +/- present(as optional) following with 1 or more occurrences.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.