title: Loop in Credit Card Validation in java
body: <p>I am a high school student in an introductory Computer Science course. Our assignment was the following:</p>
  
  <p>The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. the following method is used to verify actual credit card numbers but, for simplicity, we will describe it for numbers with 8 digits instead of 16:</p>
  
  <ul>
  <li>Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 4358 9795, then you form the sum 5+7+8+3 = 23.</li>
  <li>Double each of the digits that were not included in the preceding step. Add all the digits of the resulting numbers. For example, with the numbers given above, doubling the digits, starting with the next-to-last one, yields 18 18 10 8. Adding all the digits in these values yields 1+8+1+8+1+0+8=27.</li>
  <li>Add the sums of the two preceding steps. If the last digit of the result is 0, the number is valid. In our case, 23 + 27 = 50, so the number is valid.</li>
  </ul>
  
  <p>Write a program that implements this algorithm. The user should supply an 8-digit number, and you should print out whether the number is valid or not. <strong>If it is not valid, you should print out the value of the check digit that would make the number valid</strong>.</p>
  
  <p>I have everything done except for the part in bold. My code is listed below:</p>
  
  <pre><code>public class CreditCard 
  { 
  
      private String creditCardNumber;
      private boolean valid;
      private int checkDigit;
      int totalSum;
  
      /**
       * Constructor for objects of class CreditCard
       */
      public CreditCard(String pCreditCardNumber)
      {
          creditCardNumber = pCreditCardNumber;
          checkDigit = Integer.parseInt(pCreditCardNumber.substring(creditCardNumber.length() - 1));
          int sumOfDigits = checkDigit + Integer.parseInt(pCreditCardNumber.substring(6,7)) + Integer.parseInt(pCreditCardNumber.substring(3,4)) + Integer.parseInt(pCreditCardNumber.substring(1,2));
          int dig7 = Integer.parseInt(pCreditCardNumber.substring(7,8));
          int dig5 = Integer.parseInt(pCreditCardNumber.substring(5,6));
          int dig3 = Integer.parseInt(pCreditCardNumber.substring(2,3));
          int dig1 = Integer.parseInt(pCreditCardNumber.substring(0,1));
  
          String string7 = Integer.toString(dig7);
          int doubledDig7a = Integer.parseInt(string7.substring(0));
          int doubledDig7b = 0;
          if (dig7 * 2 &gt;= 10)
  
          {
              doubledDig7a = Integer.parseInt(string7.substring(0));
              doubledDig7b = 0;
          }
  
          String string5 = Integer.toString(dig5);
          int doubledDig5a = Integer.parseInt(string7.substring(0));
          int doubledDig5b = 0;
          if (dig5 * 2 &gt;= 10)
  
          {
              doubledDig5a = Integer.parseInt(string5.substring(0));
              doubledDig5b = 0;
          }
  
          String string3 = Integer.toString(dig3);
          int doubledDig3a = Integer.parseInt(string3.substring(0));
          int doubledDig3b = 0;
          if (dig3 * 2 &gt;= 10)
  
          {
              doubledDig3a = Integer.parseInt(string3.substring(0));
              doubledDig3b = 0;
          }
  
          String string1 = Integer.toString(dig1);
          int doubledDig1a = Integer.parseInt(string1.substring(0));
          int doubledDig1b = 0;
          if (dig1 * 2 &gt;= 10)
  
          {
              doubledDig1a = Integer.parseInt(string1.substring(0));
              doubledDig1b = 0;
          }
  
  
          int doubleDigits = doubledDig1a + doubledDig1b + doubledDig3a + doubledDig3b + doubledDig5a + doubledDig5b + doubledDig7a + doubledDig7b;
  
          totalSum = sumOfDigits + doubleDigits;
  
          if (totalSum % 10 == 0)
          {
              valid = true;
          }
          else
          {
              valid = false;
          }
  
      }
  
      public void makeItValid()
      {
         while (totalSum % 10 != 0)
         {
             checkDigit--;
             if (totalSum % 10 == 0)
             {
                 break;
              }
          }
      }
  
  
      public boolean isItValid()
      {
          return valid;
      }
  }
  </code></pre>
  
  <p>The loop is what I am having issues with. I always end up in an infinite loop whenever it compiles. It looks like everything should work, though. It's supposed to decrease the value of the check Digit (not increase so I don't end up with a check digit of 10 or higher), and then add that number back into the total sum until the total sum is divisible by 10, and then the loop would end. Is the type of loop I'm using wrong? Any advice would be appreciated. </p>