Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

URGENT! Java Challenge Help.

Hi guys, I really need help with this Java challenge for my classes.

Array a of ints contains the digits of a UPC code (Not including the check digit). Complete the following code to store the check digit in checkdigit.

The code below performs the first two steps in a single for loop that traverses the array a from end to start. The int pos keeps track of the position (going from right to left) of the digit currently being processed. Use the modulus operator to help you find out if pos is even or odd.

Thanks so much if you're able to help me <3

// Enter values to test here
Int[]a = { 1,5,4,7 };
int checkdigit;
int stepsTotal = 0;
int pos = 1;

// Loop over array from end to start
for ( int i = a.length - 1; i >= 0; i-- )
{
  if ( /* Your code here*/;)
    stepsTotal += /* Your code here*/;
  else
    stepsTotal += /* Your code here*/;;

  pos++;
}

// The closest multiple of 10 <= stepsTotal
int multipleOf10 = 10 * ( stepsTotal / 10 );

// If necessary, adjust to the closest multiple of 10 >= stepsTotal
if ( multipleOf10 != stepsTotal )
  multipleOf10 += /* Your code here */ ;

checkdigit = /* Your code here */ - stepsTotal;

2 Answers

Hi Harry,

I will try to help (posting your original code first for reference). I'm not sure what is expected for the part after the for-loop. Maybe you could give us how UPC check digits are calculated?

// Enter values to test here
Int[]a = { 1,5,4,7 };
int checkdigit;
int stepsTotal = 0;
int pos = 1;

// Loop over array from end to start
for ( int i = a.length - 1; i >= 0; i-- )
{
  if ( /* Your code here*/;)
    stepsTotal += /* Your code here*/;
  else
    stepsTotal += /* Your code here*/;;

  pos++;
}

// The closest multiple of 10 <= stepsTotal
int multipleOf10 = 10 * ( stepsTotal / 10 );

// If necessary, adjust to the closest multiple of 10 >= stepsTotal
if ( multipleOf10 != stepsTotal )
  multipleOf10 += /* Your code here */ ;

checkdigit = /* Your code here */ - stepsTotal;

I believe there is a type in the initialization of the array:

// Enter values to test here
int[] a = { 1,5,4,7 }; //over here
int checkdigit;
int stepsTotal = 0;
int pos = 1;

// Loop over array from end to start
for ( int i = a.length - 1; i >= 0; i-- )
{
  if ( /* Your code here*/;)
    stepsTotal += /* Your code here*/;
  else
    stepsTotal += /* Your code here*/;;

  pos++;
}

// The closest multiple of 10 <= stepsTotal
int multipleOf10 = 10 * ( stepsTotal / 10 );

// If necessary, adjust to the closest multiple of 10 >= stepsTotal
if ( multipleOf10 != stepsTotal )
  multipleOf10 += /* Your code here */ ;

checkdigit = /* Your code here */ - stepsTotal;

Now to check if a value from the array is even or odd:

// Enter values to test here
int[] a = { 1,5,4,7 }; //over here
int checkdigit;
int stepsTotal = 0;
int pos = 1;

// Loop over array from end to start
for ( int i = a.length - 1; i >= 0; i-- )
{
  if (pos % 2 == 0)   // the remainder after dividing by 2 is 0, so pos is even
    stepsTotal += /* Your code here*/;
  else // the value of pos is odd
    stepsTotal += /* Your code here*/;;

  pos++;
}

// The closest multiple of 10 <= stepsTotal
int multipleOf10 = 10 * ( stepsTotal / 10 );

// If necessary, adjust to the closest multiple of 10 >= stepsTotal
if ( multipleOf10 != stepsTotal )
  multipleOf10 += /* Your code here */ ;

checkdigit = /* Your code here */ - stepsTotal;

That's as far as I can get without the entire problem statement/possibly needing more information about UPC codes. I hope this helps.

Thanks for your help, i'm still trying to figure out for myself about the UPC codes.

I'll let you know if I get any more information / am able to figure it out.

Thanks again :)

Harry,

UPC codes and their check digit have a known algorithm. I will do an example mathematically and leave the coding for you to complete.

The UPC given without the check digit included is:

1547_<- check digit not in last position for our algorithm. UPC code check digits are usually here.

For every odd position in the array, add the digits together.

1 + 4 = 5

Multiply the total by three.

5 * 3 = 15

For every even position in the array, add the digits together.

5 + 7 = 12

Add the two results together.

15 + 12 = 27

Compute the modulo 10.

27 modulo 10 = 7

Subtract from 10.

10 - 7 = 3

The check digit is 3. 15473 is the UPC code with check digit included.

I hope this helps.

Regards,

Chris

Thank you! Makes alot more sense.