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

  • Home
  • SEARCH
  • 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 8542203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:00:27+00:00 2026-06-11T12:00:27+00:00

Help me to understand how this code works. It essentially adds commas into a

  • 0

Help me to understand how this code works. It essentially adds commas into a string of numbers. So if the user types a 1 to 3 digit number it is unchanged. For a four digit number ,it adds a comma so

  • 1111 becomes 1,111
  • 11111 becomes 11,111
  • 111111111 becomes 11,111,111

and so on. Here’s the code:

private String addCommasToNumericString (String digits)
{
    String result = "";
    int len = digits.length();
    int nDigits = 0;

    for (int i = len - 1; i >= 0; i--)                      
    {
        result = digits.charAt(i) + result;                 
        nDigits++;                                          
        if (((nDigits % 3) == 0) && (i > 0))                
        {
            result = "," + result;
        }
    }
    return (result);
}

I´ll explain what I do understand of it

The for loop basically counts the length of the number the user has written to avoid putting a comma before the first number (e.g. ,1111). And while i is less than the length of the string it subtracts 1.

result returns the char at position i, since it counts downwards it returns the chars “opposite” from right towards left.

nDigits adds 1 from to the initial value of 0 on each iteration through the loop.

I guess now is where I am having trouble seeing exactly what is going on: if ("nDigits % 3) == 0.

So for the two first iteration through loop it will not execute the if loop because:

  • 1 % 3 = 1
  • 2 % 3 = 2
  • 3 % 3 = 0

nDigits starts out as 1 because of the nDigits++ code inside the for loop, so how does it put the comma after three digits and not two? And how does it know when there is only 4 or 5 digits to place the comma corretly at position 1 and two (1,111 – 11,111)?

  • 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-06-11T12:00:28+00:00Added an answer on June 11, 2026 at 12:00 pm

    I think the easiest way to explain this is to slow it down to each pass.

    The loop starts at the end of the string so if you have the string 12345, then after the first time through the loop result will be “5” and nDigits will be 1.

    The next time through, ‘4’ will be added to the front of the result giving you “45” and nDigits will be 2.

    The third time through, it adds ‘3’ to the front of result making that “345” and then the if-then triggers and adds a comma to the front. Result is now “,345”.

    More passes will give you “12,345”.

    I think what is confusing you is that loop starts at the ‘5’ and not at the ‘1’. Everything is added to the front of result and not to the end as you would normally expect.

    Hope this helps!

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

Sidebar

Related Questions

How this code works? any one help me to understand ? var myArray =
Can someone explain help me understand how the this bit of code works? Particularly
I am trying to understand how this piece of self-replicating code works (found here
Hi was I hoping someone would help understand this behaviour of the below code.
I can't understand why this code works: @ads = Ads.find( :all, :joins => INNER
Please help me understand this recursive function... var stack = Array; function power(base, exponent){
I'm a noob; please help me understand this authentication config / bindings stuff that
Can someone help me understand how to write this case statement properly its not
Can you help me understand how I should debug this errors? What information would
can anybody help me understand how Honda achieved this effect: http://testdays.hondamoto.ch/ I mean 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.