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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:01:40+00:00 2026-05-31T09:01:40+00:00

I am having some trouble with modifying Strings to be space delimited under the

  • 0

I am having some trouble with modifying Strings to be space delimited under the special case of adding spaces to all non-numerical characters.

My code must take a string representing a math equation, and split it up into it’s individual parts. It does so using space delimits between values This part works great if the string is already delimited.
The problem is that I do not always get a space delimited input. To deal with this, I want to first insert these spaces so that the array is created properly.

What my code must do is take any character that is NOT a number, and add a space before and after it.

Something like this:
3*24+321 becomes 3 * 24 + 321
or
((3.0)*(2.5)) becomes ( ( 3.0 ) * ( 2.5 ) )

Obviously I need to avoid inserting space in the numbers, or 2.5 becomes 2 . 5, and then gets entered into the array as 3 elements. which it is not.

So far, I have tried using

String InputLineDelmit = InputLine.replaceAll(“\B”, ” “);

which successfully changes a string of all letters “abcd” to “a b c d”
But it makes mistakes when it runs into numbers. Using this method, I have gotten that:

(((1)*(2))) becomes ( ( (1) * (2) ) ) —- * The numbers must be separate from parens

12.7+3.1 becomes 1 2.7+3.1 —– * 12.7 is split

51/3 becomes 5 1/3 —– * same issue

and 5*4-2 does not change at all.

So, I know that \D can be used as a regular expression for all non-numbers in java. However, my attempts to implement this (by replacing, or combining it with \B above) have led either to compiler errors or it REPLACING the char with a space, not adding one.

EDIT:

==== Answered! ====

It wont let me add my own answer because I’m new, but an edit to neo108’s code below (which, itself, does not work) did the job. What i did was change it to check isDigit, not isLetter, and then do nothing in that case (or in the special case of a decimal, for doubles). Else, the character is changed to have spaces on either side.

public static void main(String[] args){


    String formula = "12+((13.0)*(2.5)-17*2)+(100/3)-7";

    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < formula.length(); i++){

        char c = formula.charAt(i);
        char cdot = '.';

        if(Character.isDigit(c) || c == cdot) {
        builder.append(c);
        }
        else {
        builder.append(" "+c+" ");
        }
    }   

    System.out.println("OUTPUT:" + builder);


}

OUTPUT: 12 + ( ( 13.0 ) * ( 2.5 ) – 17 * 2 ) + ( 100 / 3 ) – 7

However, any ideas on how to do this more succinctly, and also a decent explanation of StringBuilders, would be appreciated. Namely what is with this limit of 16 chars that I read about on javadocs, as the example above shows that you CAN have more output.

  • 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-31T09:01:41+00:00Added an answer on May 31, 2026 at 9:01 am

    Something like this should work…

    String formula = "Ab((3.0)*(2.5))";
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < formula.length(); i++){
        char c = formula.charAt(i);
        if(Character.isLetter(c)) {
            builder.append(" "+c+" ");
        } else {
            builder.append(c);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m having some trouble retrieving a collection of strings in a projection: say that
I'm having some trouble getting my ViewModel to return a non-null object in the
I'm having some trouble with modifying qTip's tip size (x,y). I tried to add
Having some trouble w/ IE7. IE8, Chrome, Firefox all work fine, but IE7 won't
I'm having some trouble understanding why modifying a property in instance a modifies the
I'd like some help with some URL rewriting magic. I am having trouble modifying
I'm doing some contribution to an open source library, but I'm having trouble modifying
Having some trouble with what should be a very simple scenario. For example purposes,
Hey having some trouble trying to maintain transparency on a png when i create
Basically having some trouble with using Hover to hide or show an item. 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.