CCNumber.java.html

  1  /**
  2     This program removes spaces and dashes from a credit card number.
  3  */
  4  public class CCNumber
  5  {
  6     public static void main(String[] args)
  7     {
  8        String creditCardNumber = "4123-5678-9012-3450";
  9        
 10        int i = 0;
 11        while (i < creditCardNumber.length())
 12        {
 13           char ch = creditCardNumber.charAt(i);
 14           if (ch == ' ' || ch == '-')         
 15           {
 16              // Remove the character at position i
 17  
 18              String before = creditCardNumber.substring(0, i);
 19              String after = creditCardNumber.substring(i + 1);
 20              creditCardNumber = before + after;
 21           }
 22           else
 23           {
 24              i++;
 25           }
 26        }
 27        
 28        System.out.println(creditCardNumber);      
 29     }
 30  }