The next iteration is here.
I have this small method for printing long integers neatly. For example:
neatify(123L) = "123"
neatify(1234L) = "1 234"
neatify(12345L) = "12 345"
.
.
.
The code:
import java.util.Scanner;
public class Main {
public static String neatify(final long number,
final int groupLength) {
final String str = Long.toString(number);
if (groupLength < 1) {
return str;
}
final char[] charArray = str.toCharArray();
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < charArray.length; ++i) {
if (i != 0 && (charArray.length - i) % groupLength == 0) {
sb.append(' ');
}
sb.append(charArray[i]);
}
return sb.toString();
}
public static String neatify(final long number) {
return neatify(number, 3);
}
public static void main(final String... args) {
final Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLong()) {
System.out.println(neatify(scanner.nextLong()));
}
}
}