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 6623865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:35:01+00:00 2026-05-25T21:35:01+00:00

I am working on a personal project and I want to take in userinput

  • 0

I am working on a personal project and I want to take in userinput that looks like this :

   1.0+2.5+3--4 

and format it to something like this :

   1.0 + 2.5 + 3 - -4  

so far I am using the .replace(“+”) to .replace(” + “) and doing that for all of the operands but the problem is it makes the user input into this:

  1.0 + 2.5 + 3 - - 4 

Is there a way that I can make it with the negative signs. I want to do this so I could parse the numbers into doubles and add and subtract them later on.

my code for it :

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


public class StringMan {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    String check = "-a1 +a2 +       a3 +-a5";
    check  = check.replace("--", "+");
    System.out.println(check);
    Pattern pattern = Pattern.compile("\\s+");
      Matcher matcher = pattern.matcher(check);
      boolean expr = matcher.find();
      String str = matcher.replaceAll(" ");
      System.out.println(str);



}

   }

output is:

   -a1 +a2 -       a3 +-a5
   -a1 +a2 - a3 +-a5

the problem is I want the output to look like this:
-a1 + a2 – a3 + -a5

  • 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-25T21:35:01+00:00Added an answer on May 25, 2026 at 9:35 pm

    I would recommend using regular expressions and their “group” functionality. I would actually remove all whitespace to make things easier, take it out of the equation, one less thing to deal with. And obviously I would recommend simplifying the string, replacing “–” with “+”, “*+” with “*” and so on.

    now you can use a regex on your cleaned up string.

    Pattern firstPat = Pattern.compile("(((\\+|-)?)\\d+(.\\d+)?)");//for matching the first number, leading sign is optional
    Pattern remainingPat = Pattern.compile("(\\+|-)(\\d+(.\\d+)?)");//for remaining numbers, leading sign is mandatory.
    Pattern remainingPatWithExtOps = Pattern.compile("(\\*|/|\\+|-)(-?\\d+(.\\d+)?)");//for remaining numbers, accommodating multiply and divide with negative signs(positive signs should have been cleaned out)
    
    Matcher match = firstPat.matcher(inputString);
    

    now you can iterate through the string using the match.find() method. and then use match.group(1) to get the sign/operation, and use match.group(2) to get the number…

    So…

    Double firstnum;
    boolean firstNumSigned = false;
    if(match.find())
    {
        firstNum = Double.parse(match.group(0));// Parsing handles possible sign in string. 
        //obv check for exceptions during this and double check group num
        String tmp = match.group(1);
        firstNumSigned = tmp.equals("+") || tmp.equals("-");
    }
    else
    {//no match means the input was probably invalid....
        throw new IllegalArgumentException("What the heck were you thinking inputting that?!");
    }
    match = remainingPat.matcher(inputString);//use our other pattern for remaining numbers
    if(firstNumSigned)
    {
        match.find();//a signed first number will cause success here, we need to ignore this since we already got the first number
    }
    Double tmpRemaingingNum;
    String operation;
    while(match.find())
    {
        operation = match.group(1);
        tmpRemainingNum = Double.parse(match.group(2));
        //Do what you want with these values now until match.find() returns false and you are done
    }
    

    PS: code is not tested, im fairly confident of the regex, but I’m not 100% sure about the grouping brackets on the first pattern.. might need to experiment

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a small personal multiplayer game project. I am using
I am working on a personal project with F# and would like to experiment
I am working on a personal project that uses a custom config file. The
Working on my personal knowledge base... I want to format my documents, even on
I'm working on a personal project in Visual Studio 2010 Express edition, using ASP.NET
I am working on a personal project, i want to learn how to create
I'm working on a project that tracks blobs (people). We want to track their
I am planning to begin working on my first personal project this June: a
For a personal project I'm working on right now I want to make a
So I am working on a personal MVC project for practice. I like the

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.