2

I have a string which represents count "1,125,854".

I want to check if "," is present after every thousand decimal.

e.g. 125,854 and 1,125,854

I have written following code

import java.text.DecimalFormat;
import java.text.Format;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

public class CountComma {
    public static void main(String[] args) {
        String str = "1,125,854";
        int count = 0;
        String revStr = new StringBuilder(str).reverse().toString();
        System.out.println("Reverse String: " + revStr);
        List<Integer> format = new ArrayList<Integer>();

        for (char ch : revStr.toCharArray()) {
            System.out.println(ch);
            if (ch == ',') {

                count = count + revStr.indexOf(ch);
                format.add(count);

            }

        }

        System.out.println("Count: " + count);

        System.out.println(format.toString());
    }
}

This code gives output :
Reverse String: 458,521,1
4
5
8
,
5
2
1
,
1
Count: 6
[3, 6]

Could anyone please suggest better way for the same?

Thanks

Vishal
  • 35
  • 5
  • What's your problem? Is your code doing what you want it to do or isn't it? – Eran Dec 15 '16 at 10:28
  • A better way would be to write a method that returns a boolean true/false depending on whether input is valid or not. Right now, what are you checking? What are you printing? – Erwin Bolwidt Dec 15 '16 at 10:29
  • Why do you want to do it? do want to parse the string into an int value? If so, see - http://stackoverflow.com/questions/11973383/how-to-parse-number-string-containing-commas-into-an-integer-in-java – Gurwinder Singh Dec 15 '16 at 10:37
  • Thanks all for your suggestions. My problem was : "check if a number string has correct comma formatting" Its solved after applying regular expression.("\\d{1,3}(,\\d{3})*") – Vishal Dec 17 '16 at 18:15

3 Answers3

5

If all you want to do is check if a number string has correct comma formatting, then you can use this one-liner:

String str = "1,125,854";
boolean isCorrect = str.matches("\\d{1,3}(,\\d{3})*");

Update:

If you wanted to make the regex more locale agnostic, you could first get the grouping separator for thousands based on the current locale. For example, if doing this check in a servlet you might try this:

Locale currentLocale = httpServletRequest.getLocale();
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(currentLocale);
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();
char separator = symbols.getGroupingSeparator();

and then

boolean isCorrect = str.matches("\\d{1,3}(" + separator + "\\d{3})*");
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • The anchors are not necessary. – Robin Koch Dec 15 '16 at 10:41
  • I don't know where the string will come from and how the OP is going to use that string, but it might be useful to check the locale and use the proper grouping separator instead of comma – BackSlash Dec 15 '16 at 10:48
0

You can use RegEx for that (tutorial here: http://www.vogella.com/tutorials/JavaRegularExpressions/article.html).

(\d{1,3})(,(\d{3}))*

(fixed my answer as pointed out by Robin Koch)

pringi
  • 3,987
  • 5
  • 35
  • 45
0

You should really use NumberFormat with the correct Locale: https://docs.oracle.com/javase/8/docs/api/java/text/NumberFormat.html or DecimalFormat constructor that takes a pattern: https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html#DecimalFormat-java.lang.String-

The parse method will throw a ParseException when the string does not match the format.