6 Replies Latest reply: Jan 25, 2013 7:24 AM by 962826

# Barcode problem

Hi - I have to enter a string of numbers (at least 5) that will represent a barcode and check for correctness using an algorithm. I am having problems just getting started.

<code>
public static void main(String[] args) {
int x = 0;

// Enter a a barcode number, must be at least 5 digits
Scanner input = new Scanner(System.in);
System.out.println("Please enter a number that contains at least 5 digits: ");

//Create an array
double[] myBarcode = new double[x];

//Store the numbers in array
for (int index = 0; index < myBarcode.length; index++){
myBarcode[index] = input.nextDouble();
}

}
}
<code>

How do I enter an unspecified amount of numbers as a string and then store them in an array without knowing what the amount of numbers is even going to be? Additionally, how would include something in the program to verify that each character is a numeric digit?
• ###### 1. Re: Barcode problem
Hi, read the announcement on formatting code.

You read the entered numer as a String. The String consists of n characters. Use the Character class and it's methods or use any non primitive 'number' class to parse the characters.
• ###### 2. Re: Barcode problem
PhHein wrote:
Hi, read the announcement on formatting code.

You read the entered numer as a String. The String consists of n characters. Use the Character class and it's methods or use any non primitive 'number' class to parse the characters.
What if I took this approach:
``````public static void main(String[] args) {
// Create a scanner
Scanner input = new Scanner(System.in);

// Prompt the user to enter at least a 5 digit string
System.out.println("Please enter a number that contains at least 5 digits: ");
String  barcode = input.nextLine();
}
}``````
Very novice here - thanks for your help.

So I have the string in there now, and I don't have to stroe it in an array - I can use the character class to reference specific numbers right?
• ###### 3. Re: Barcode problem
I probably should add a few more details to this. The algorithm to see if this is a valid bar code is as follows:

1. Add all digits located in odd numbered positions (counting from left to right with the leftmost character being position 0, excluding the check digit, which is the last digit.

2. Multiply each digit in an even numbered location (excluding the check digit) by 2. If the result of the multiplication exceeds 9, add the two digits. Add the sum of these results with the sum calculated in Step 1.

3. Divide the result of step 2 by 10 and subtract the remainder from 10. If the result is less than 10, it should be equal to the check digit. Otherwise, the check digit should be zero.

Write a java program that will accept a string of characters representing a bar code and check it for correctness using the algorithm given above. The program should also verify that each character is a numeric digit.

That being said, I have the code to enter the string of numbers but then am stumped. This is part 2 of an intro class I am in, and I had winter break so my brain needs refreshed.
• ###### 5. Re: Barcode problem
restless8052 wrote:
So I have the string in there now, and I don't have to stroe it in an array - I can use the character class to reference specific numbers right?
You have the line entered by the user in your barcode variable. You can access individual number with barcode.charAt(n) method.

You'll need to convert the characters into numbers, which can be done with for example
``int num = barcode.charAt(0)-'0';``
Then you can do your calculations.

Also include the checks that the string is correct length and contains only digits (using for example Character.isDigit() or trying to parseInt() the String).
• ###### 6. Re: Barcode problem
I just want to share how I solved this. Went to a tutor for help. Thanks for all the suggestions, though!
``````public class barcodes {

public static void main(String[] args) {
int checkDigit = 0;

// Create a scanner
Scanner input = new Scanner(System.in);

// Prompt the user to enter at least a 5 digit string
System.out.println("Please enter a number that contains at least 5 digits: ");
String  barcodeString = input.nextLine();

// Break program if less than 5 digits
if (barcodeString.length() < 5){
System.out.println("Bar code must be at least 5 digits.");
System.exit(0);
}

checkDigit = Integer.parseInt(barcodeString.substring(barcodeString.length()-1, barcodeString.length()));

// Go through string length

int oddSum = 0;
int evenSum = 0;
int evens = 0;
int finalSum = 0;

for (int i = 0; i < barcodeString.length()-1; i++){
// odd remainder is 1
if (i%2 == 1){
oddSum += Integer.parseInt(barcodeString.substring(i, i+1));
//     System.out.println("This is the oddSum " + oddSum);
// even remainder is 0
}else if (i%2 == 0) {
int evens2 = Integer.parseInt(barcodeString.substring(i, i+1)) * 2;
int evens3 = 0;
if (evens2 > 9) {
evens3 = Integer.parseInt((evens2 + "").substring(0, 1)) + Integer.parseInt((evens2 + "").substring(1, 2));
}//evens2 = evens3 + oddSum;

else {
evens3 = evens2;
}
evenSum += evens3;
// System.out.println("This is the evenSum " + evenSum);
}

}

finalSum = oddSum + evenSum;
// System.out.println(finalSum);

int finalFinal = finalSum = 10 - finalSum%10;

if ((finalFinal < 10) && finalFinal == checkDigit){
System.out.println("This is a barcode.");
}
else if ((finalFinal >= 10) && checkDigit == 0){
System.out.println("This is a bar code.");
}
else {System.out.println("This is not a bar code.");
}
// System.out.println(finalSum);
}
}``````