I want to read in a 6 digit number e.g 123456 using the scanner and store each digit in an array of digits.
System.out.println("Please enter a 6 digit code");
int code = keyboard.nextInt();
And then somehow put that into:
int [] array = {1,2,3,4,5,6};
I need the array to be int type because I am going to find the product when I add together the odd numbers in the sequence (1+3+5) and add the even numbers (2+4+6) (as per specifications of the program) I then have some conditions that tell me which number to concatenate onto the end.
I can achieve this by pre-defining an array of numbers but I want this number to come from the user.
Unless there is another way of doing this without using arrays? I appreciate any pointers.
Here is my code when i have pre-defined the array and it's values.
int code[] = {1,2,3,4,5,6};
//add the numbers in odd positions together
int sum_odd_position = 0;
for (int i=0; i<6; i+=2)
{
sum_odd_position += code[i];
}
//convert the initial code into strings
String string_code = "";
String seven_digit_code = "";
//add numbers in even positions together
int sum_even_position=0;
for (int i=1; i<6; i+=2)
{
sum_even_position += code [i];
}
//add them both together
int sum_odd_plus_even = sum_odd_position + sum_even_position;
//calculate remainder by doing our answer mod 10
int remainder = sum_odd_plus_even % 10;
System.out.println("remainder = "+remainder);
//append digit onto result
if (remainder==0) {
//add a 0 onto the end
for (int i=0; i<6; i+=2)
{
string_code += code [i];
}
}
else {
//subtract remainder from 10 to derive the check digit
int check_digit = (10 - remainder);
//concatenate digits of array
for (int i=0; i<6; i++)
{
string_code += code[i];
}
//append check digit to string code
}
seven_digit_code = string_code + remainder;
System.out.println(seven_digit_code);
}
}