Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6173671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:37:02+00:00 2026-05-23T23:37:02+00:00

I’m trying to learn about regular expressions but am not doing so well after

  • 0

I’m trying to learn about regular expressions but am not doing so well after reading through the java tutorial.

This program is supposed to take an imput in the format:

a) add dd dd together
b) subtract 05 from 13
c) add 02 to 03

And return the dd (+ or -) dd = answer

The (wrong) way I set this up is to have the prog try to find either of the 3 matches, and continue to do so until the user inputs “bye.” If there isn’t a match it should just prompt the user for an input again.

Here’s my code! With exactly 100 errors. :/
If anyone can help me with the syntax, it’d really be appreciated!

 import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;

    class Calculator {
      public static void main(String[] args){
        Scanner imp = new Scanner(System.in);
        System.out.println("yes> ");
        String s = imp.nextLine();

        if (s.equals("bye")) {
          System.exit(0);
      }

        while (true) {

          Pattern p = Pattern.compile(s); //compile string, check for formats

          Matcher x = p.matcher(\badd\b\s\d\d\s\d\d\s\btogether\b); //format add 12 12 together
          Matcher y = p.matcher(\bsubtract\b\s\d\d\s\d\d\s\bfrom\b); //format subtract 05 from 13
          Matcjer z = p.matcher(\badd\b\s\d\d\s\bto\b\s\d\d); //format add 02 to 03

          boolean b = p.matches;

          boolean l = x.matches;
          boolean i = y.matches;
          boolean g = z.matches; 

          if (l.equals(true))
            return (\d\d " + " \d\d " = " \d\d+\d\d);
          else if (i.equals(true))
            return (\d\d " + " \d\d " = " \d\d-\d\d);
          else if (g.equals(true))
            return (\d\d " + " \d\d " = " \d\d+\d\d);
        }
      }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:37:03+00:00Added an answer on May 23, 2026 at 11:37 pm

    ugh where to begin…

    first off Pattern.compile() is expecting the regex (the format strings) while matcher() expects the string to test against

    @Samir has shown you what was wrong with the regexes in the code itself (I edited them a bit for more clarity)

    l.matches needs ()

    you cannot call methods on primitive boolean variables if(b) is sufficient to test if it is true or not

    and to get specific submatches you need to use capturing groups

    to concatenate strings together you can use +

    to output something to the console System.out.println should be used not return

    with the most obvious errors solved:

    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    
    class Calculator {
      public static void main(String[] args){
        Scanner imp = new Scanner(System.in);
        System.out.println("yes> ");
    
    
        while (true) {
          String s = imp.nextLine();//put getting the input inside the loop or it's infinite
    
          if (s.equals("bye")) {
              System.exit(0);
          }
    
          Matcher x = Pattern.compile("add\\s(\\d+)\\s(\\d+)\\stogether").matcher(s); //format add 12 12 together
          Matcher y = Pattern.compile("subtract\\s(\\d+)\\sfrom\\s(\\d+)").matcher(s); //format subtract 05 from 13
          Matcjer z = Pattern.compile("add\\s(\\d+)\\sto\\s(\\d+)").matcher(s); //format add 02 to 03
    
    
          boolean l = x.matches();
          boolean i = y.matches();
          boolean g = z.matches(); 
    
          if (l){
            System.out.println(l.group(1) + " + " + l.group(2) + " = " + 
                 (Integer.parseInt(l.group(1))+Integer.parseInt(l.group(2))) );
          }else if (i){
            System.out.println(i.group(1) + " - " + i.group(2) + " = " + 
                 (Integer.parseInt(i.group(1))+Integer.parseInt(i.group(2))) );
          }else if (g){
            System.out.println(g.group(1) + " + " + g.group(2) + " = " + 
                 (Integer.parseInt(g.group(1))+Integer.parseInt(g.group(2))) );
          }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.