2

The assignment is to add comes after every three digits looking from right to left. So number 1000000 should be 1,000,000 etc.

I have relatively good idea how to tackle this problem, but I have no idea why am I getting no output. Maybe I am making some mistake that I am not aware of or something...

I think that I understand concept that Strings are immutable, so they cannot be changed in place and when you want to change something to string you have to make new string object. But I dont get how is this possible then:

`result = ch + result;` 

and this

result = "," + result;

What am I getting wrong here ?

   import acm.program.*;
   import acm.util.*;

    public class AddCommasToNumericString extends ConsoleProgram{

        public void run(){
            while(true){
                String digits = readLine("Enter a numeric string: ");
                if (digits.length() == 0) break;
                println(addCommasToNumericString(digits));
            }
        }

        public String addCommasToNumericString(String digits){
            String result = "";
            int counter = 0;
            for (int i = digits.length()-1; i <= 0 ; i--){
                char ch = digits.charAt(i);
                result = ch + result;
                counter++;
                if (counter % 3 == 0){
                    result = "," + result;
                }
            }
            return result;
        }
    }
  • 1
    `NumberFormat.getNumberInstance(Locale.US).format(digits)` will do the job. – bhantol Jan 03 '17 at 23:29
  • @bhantol Yes of course, but I suspect his assignment is to do this manually to learn how to work with strings. – Tim Biegeleisen Jan 03 '17 at 23:30
  • Yup thats why I asked so that I don;t post this easy solution. – bhantol Jan 03 '17 at 23:34
  • In case you haven't already found the answer to why you can do `result = "," + result` when strings are immutable, it's because all variables are _references_ to objects and are not objects themselves. So when you say `result = something...` you are only setting the variable `result` to refer to a new string; you are not actually changing the string itself. – Klitos Kyriacou Feb 02 '17 at 19:35

2 Answers2

3

I suggest eliminating the counter and use only the loop variable by making a small change:

public String addCommasToNumericString(String digits) {
    String result = "";
    for (int i=1; i <= digits.length(); ++i) {
        char ch = digits.charAt(digits.length() - i);
        if (i % 3 == 1 && i > 1) {
            result = "," + result;
        }
        result = ch + result;
    }

    return result;
}

addCommasToNumericString("123");     // 123
addCommasToNumericString("12345");   // 12,345
addCommasToNumericString("1234567"); // 1,234,567
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    Every time is like this, I complete some assignment and always make these goddamn typing mistakes! I Tnx Tim. –  Jan 03 '17 at 23:33
3

Convert the read line into an Integer, then format it as a String

String digits = readLine("Enter a numeric string: ");
Integer myInt = new Integer(digits);
String output = NumberFormat.getNumberInstance(Locale.US).format(myInt.value());
Raul Cacacho
  • 267
  • 1
  • 4
  • 15
older coder
  • 614
  • 1
  • 4
  • 12