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