0
Scanner input = new Scanner(System.in);
String girdi = input.nextLine();
for(int i=1; i<girdi.length(); i+=1){
    if(girdi.charAt(i) == '*'){
        int temp1= girdi.charAt(i-1)-'0';
        int temp2= girdi.charAt(i+1)-'0';
        int temp3 = temp1 * temp2;
        String a = girdi.substring(i-1,i);
        String b = "" + temp3;                
        String a2 = girdi.substring(i+1,i+2);
        String b2 = girdi.substring(i , i+1); 
        girdi = girdi.replaceFirst(a,b);
        girdi = girdi.replaceFirst(b2, "");   
        girdi = girdi.replaceFirst(a2, "");     
    }
}

I am just trying to make multiplication

when I type something like that (3*5) give me that error:

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0

and when I am using backslah to escape :

if(s.charAt(i) == '\\*'){ 

I see unclosed character literal and illegal start of expression errors

what should I do?

6
  • 3
    No, it's not this part of the code doing that. No regexes here. Commented Nov 18, 2016 at 21:56
  • 2
    You're first example should not throw an exception, please edit your question to include your code in full. Commented Nov 18, 2016 at 21:56
  • Please edit your question and include all lines of the exception’s stack trace. Commented Nov 18, 2016 at 22:00
  • @Agarta what do you mean "give an input"? If s is "4 * 5", you've still got no regexes in the code you've shown. Commented Nov 18, 2016 at 22:03
  • @Agarta you need to show a minimal reproducible example. The code you have shown doesn't produce the error you describe. Commented Nov 18, 2016 at 22:12

1 Answer 1

0

Refer to the Javadoc of String.replaceFirst:

  • The first parameter is a regular expression (meaning characters like * have special meaning);
  • The second parameter is a regex replacement (meaning certain character sequences have special meaning, e.g. \ and $).

If you want to pass in strings from an untrusted source (like user input), you need to quote them appropriately:

girdi = girdi.replaceFirst(Pattern.quote(a), Matcher.quoteReplacement(b));

However, String.replaceFirst is not the right tool for this job. It won't work correctly if there is a match for the search string before the one you are really looking for.

The most straightforward modification to your code would be to use String.substring - you know exactly where the thing you want to replace is, so make use of that information:

girdi = girdi.substring(0, i-1) + temp3 + girdi.substring(i+2);
i 

(This would replace the last 7 lines in the loop)

You can do this better again by using a StringBuilder, to avoid creating unnecessary strings in the loop.

However, this still wouldn't work in the case of multi-digit or negative numbers.

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.