fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String initial[] = {"djsndni123412ndksnd3412*&^%34(*(*1234"
  13. ,"djsndni123412ndksnd3412*&^%34(*(*123"};
  14. // remove all non-digits characters
  15. System.out.println(formatCardInput(initial[0].replaceAll("\\D", "")));
  16. System.out.println(formatCardInput(initial[1].replaceAll("\\D", "")));
  17. }
  18. private static String formatCardInput(String s){
  19. StringBuilder sb = new StringBuilder(s);
  20. System.out.println(sb);
  21. if(sb.length() == 16){
  22. int gap = 0;
  23. for (int i = 4; i< sb.length()-1;i+=4){
  24. sb.insert(i + gap,' ');
  25. gap++;
  26. }
  27. }
  28. else{
  29. sb.insert(4,' ');
  30. sb.insert(11,' ');
  31. }
  32. return sb.toString();
  33. }
  34. }
Success #stdin #stdout 0.1s 27904KB
stdin
Standard input is empty
stdout
1234123412341234
1234 1234 1234 1234 
123412341234123
1234 123412 34123