I know that UPC codes have a check digit and how to use this check digit to see if the code is valid.
If the code is not valid, my program needs to tell me where the error is. I know it can be done, but how? I need to detect single replacement errors (e.g., typing a 5 in the place of a 2) and transposition errors (e.g., typing 12 instead of 21).
Seems simple but I can't figure it out.
The following code will detect errors in a UPC-A code (12-digit) when considered a String (recommended way).
String upc = "074985003004"; // UPC as a string
int sum = 0;
for(int i = 0; i < a.length(); i++) {
sum += (i%2==0) ? (3*(upc.charAt(i)-48)) : (upc.charAt(i)-48);
}
if (sum % 10 == 0) {
System.out.println("true");
} else {
System.out.println("false");
}
Alternatively, a long can be used in the following code: (Note: the leading zero's must be removed.)
long upc = 74985003004L; // omit leading zeros;
int sum =0;
for(int i=0; i < 12; i++) {
sum += (i%2 == 0) ? (upc % 10) : (sum += 3*(upc % 10));
upc /= 10;
}
if (sum % 10 == 0) {
System.out.println("true");
} else System.out.println("false");
It is usually best to validate the UPC code before testing. i.e. check to make sure it contains the correct number of digits and only the digits 0-9. As mentioned in the link: https://en.wikipedia.org/wiki/Universal_Product_Code, UPC-A detects 100% of single digit errors and 90% of 2-digit transposition errors.
*UPC does not support error correction. However, if you know which digit is incorrect, you can just try all 10 possible values for that digit until no errors are detected (a valid UPC code).
Related
I tried to write a method (for kicks) that would sum up the digits at even places using Java recursion.
For example, the number 23495 would return 3+9 = 12.
I am unsuccessful and would appreciate hints or what I'm doing wrong.
int sumEven = 0;
int sumOdd = 0;
int i = 1;
if (n == 0)
return sumEven;
if (n != 0) {
if (i % 2 == 0)
{
i++;
sumEven += n % 10;
}
else
{
i++;
sumOdd += n % 10;
}
}
return sumEven + getEven (n/=10);
The problem is you're trying to do too much - take a look at my comment on the Q
A recursive method needs an input that contains everything it needs to work with, a return value, and an execution path where it calls itself until something happens that means it doesn't need to call itself any more - without this bit it will recourse until it overflows the stack
int sumEveryOtherDigit(int input){
if(input >= 100)
return input%10 + sumEveryOtherDigit(input/100);
else
return input%10;
}
This takes the input , and if there is any point to running again (if the input is at least 100) takes the rightmost digit plus running itself again with a smaller number
Eventually the number gets so small that there isn't any point running itself again so it just returns without running itself again and that is how the recursion stops
Now from your comment on another answer it seems you want to determine even and odd as working from the left so we need to either start with the number (1630) or the number divided by ten (23495 -> 2349) - basically to start the recursion going we always want to pass in a number with an even number of digits
int num = 23495;
int numOfDigits = (int)Math.log10(num)+ 1;
if(numOfDigits%2==0)
result = sumEveryOtherDigit(num);
else
result = sumEveryOtherDigit(num/10);
You should iterate over the digits of the input number, and then sum the remainder mod 10 only for even position digits:
int input = 23495;
input /= 10;
int sum = 0;
while (input > 0) {
sum += input % 10; // add last even digit
input /= 100; // advance by two digits, to the next even digit
}
System.out.println("sum of even digits of input is: " + sum);
This prints:
sum of even digits of input is: 12
I'd like to know how to remove numbers from an integer. For example, if I have the number 23875326, I want to remove the odd numbers, and get the result of 2826.
I've been trying to break each number to check if it's even or odd using a while loop, but I don't know how to merge the numbers into one integer. One important thing is, I'd like to do it without using strings, as it doesn't teach me anything new that way.
I actually think that dealing with a string of numbers is preferable, not only from a code readability point of view, but also possibly from a performance view. That being said, if we absolutely cannot use strings here, then it is still possible to work directly with the integer input.
We can examine each tens digit of the input number using num % 10. Should that digit be even, we can add it to the output number, otherwise do not add it. Note that at each iteration we need to scale the digit by 10 to the correct exponent.
Here is a working code snippet:
int length = (int) (Math.log10(num) + 1); // finds the number of digits
int output = 0;
int counter = 0;
for (int i=0; i < length; ++i) {
if ((num % 10) % 2 == 0) {
output += (num % 10) * Math.pow(10, counter);
++counter;
}
num = num / 10;
}
System.out.println(output);
2826
Demo
I used #Tim Biegeleisen code and changed it a bit, removed some code and changed the for loop to while loop:
int output = 0;
int counter = 0;
System.out.println("Enter a number");
int num = s.nextInt();
while (num != 0) {
if ((num % 10) % 2 == 0) {
output += (num % 10) * Math.pow(10, counter);
++counter;
}
num = num / 10;
}
System.out.println(output);`
In my computer science class, we were assigned a lab on recursion in which we have to print out a number with commas separating groups of 3 digits.
Here is the text directly from the assignment (the method has to be recursive):
Write a method called printWithCommas that takes a single nonnegative
primitive int argument and displays it with commas inserted properly.
No use of String.
For example printWithCommas(12045670); Displays 12,045,670
printWithCommas(1); Displays 1
I am really stumped on this. Here is my code so far:
public static void printWithCommas(int num) {
//Find length
if (num < 0) return;
int length = 1;
if (num != 0) {
length = (int)(Math.log10(num)+1);
}
//Print out leading digits
int numOfDigits = 1;
if (length % 3 == 0) {
numOfDigits = 3;
}
else if ((length+1) % 3 == 0) {
numOfDigits = 2;
}
System.out.print(num / power(10,length-numOfDigits));
//Print out comma
if (length > 3) {
System.out.print(',');
}
printWithCommas(num % power(10,length-numOfDigits));
}
It gets a stack overflow (which I can fix later), but it fails to print out some of the zeros, specifically the ones that are supposed to be after each comma.
I feel like I am taking this on with a completely wrong approach, but can't think of a good one. Any help would be appreciated.
Thanks in advance!
Note: power is a function I made that calculates power. First argument is the base, second is the exponent.
Here is the code I came up with, for anyone else that might be stuck on this:
public static void printWithCommas(int num) {
if (num > 999) {
printWithCommas(num/1000);
System.out.print(',');
if (num % 1000 < 100) System.out.print('0');
if (num % 1000 < 10) System.out.print('0');
System.out.print(num%1000);
}
else {
System.out.print(num);
}
}
/* when I run this code there is no error in fact output generated is also correct but I want to know what is the logical error in this code? please can any one explain what is the logical error. */
class abc
{
public static void main(String arg[]){
int sum=0;
//for-loop for numbers 50-250
for(int i=50;i<251;i++){
// condition to check if number should be divided by 3 and not divided by 9
if(i%3==0 & i%9!=0){
//individual number which are selected in loop
System.out.println(i);
//adding values of array so that total sum can be calculated
sum=sum+i;
}
}
//final display output for the code
System.out.println("the sum of intergers from 50 to 250 that are multiples of 3 and not divisible by 9 \n"+sum);
}
}
My philosophy is "less code == less bugs":
int sum = IntStream.rangeClosed(50, 250)
.filter(i -> i % 3 == 0)
.filter(i -> i % 9 != 0)
.sum();
One line. Easy to read and understand. No bugs.
Change this:
if(i%3==0 & i%9!=0){
to this:
if(i%3==0 && i%9!=0){
& = bitwise and operator
&& = logical operator
Difference between & and && in Java?
The only problems I saw were:
The variable sum was undeclared
Use && in place of &
int sum = 0;
for (int i = 50; i <= 250; i++) {
if (i % 3 == 0 && i % 9 != 0) {
System.out.println(i);
sum = sum + i;
}
}
System.out.println("the sum of intergers from 50 to 250 that are multiples of 3 and not divisible by 9 \n" + sum);
Well, instead of touching every single value from 50 to 250 like you would do here for(int i=50;i<251;i++), you can consider something like this...
int i = 48;
int sum = 0;
while(i < 250) {
i += 3;
if(i%9 != 0)
sum += i;
}
This is somewhat optimized in the sense that I am skipping over values that I know are not possible candidates.
But, there is a much bigger issue in your code. The following code block prints true, sure. But, it is a bad idea to depend on the & since that is not its job. The & is for bitwise AND whereas the && is for logical AND, which is what you are trying to do.
boolean t = true;
boolean f = false;
System.out.println(f&t);
Why?
In Java, if it is a && operation, as soon as you find the first false, you are sure that the expression will evaluate to false. Meanwhile, in your implementation, it would need to evaluate both sides. f&t will evaluate to false, but the JVM would need to look at both the f and t variables. Meanwhile, on using &&, it wouldn't even need to look at the t.
The algorithm is suppose to ask the middle letter of the English alphabet, in my program its N. At this point it asks the user whether:
The guess is correct
The letter is earlier in the alphabet.
The letter is later in the alphabet
When trying to guess Jose, it will first ask about N. I choose option 2 which then gives me G. I then choose option 3 which then gives me Q. I then choose option 2 which then gives me I. I then choose option 3 which then gives me R. If I choose option 2, it gives me R again, and basically I'm stuck between R and I. How can I fix my algorithm to properly provide all letters available?
My code is the following:
int let = 26/2;
if(n != 0) {
//Correct guess
if(n == 3) {
aLetter = alphabet[let];
actualLetter = Character.toString(aLetter);
aName.add(actualLetter);
}
//earlier in alphabetical order
else if(n == 2) {
let /= 2;
}
//later in alphabetical order
else if(n == 1) {
for(int i = let; i < 27; i++){
r += 1;
}
r /= 2;
let += r;
r=0;
}
} //done guessing
else if (n == 0) {
for(String str: aName) {
result3 += str;
}
}
Any help and or advice would be great. Thanks!
Problem 1
Say the letter of the alphabet (represented by --- etc.) being guessed is "s" (represented by +). The computer will first guess in the middle (let = 13, represented by |).
-------------|---+--------
You tell the computer to guess higher.
With the code
for(int i = let; i < 27; i++) {
r += 1;
}
r /= 2;
let += r;
r=0;
which can be shortened to
let += (26 - let) / 2;
the computer will guess halfway to the end (let = 19):
-------------|---+-|------
So far so good.
Now, you tell it to guess lower. The computer knows it's higher than 13 and lower than 19, so it should guess halfway in between. However, the code you have does not do that.
let /= 2;
makes let be (int)(19 / 2), i.e., 9.
---------|---|---+-|------
What you want is let = 16
-------------|--|-+|------
Problem 2
Let's say you successfully got to
-------------|--|-+|------
with the most recent guess being 16. You tell the computer to guess higher. With the code
let += (26 - let) / 2;
let will become 21.
-------------|--|-+|-|----
What you want is let = 18 or 19;
Solution
In general, a good algorithm should make let go up or down by "13 / 2n" each iteration, where "n" is the iteration number (starting at 0).
You should therefore use the code
int let = 0;
int iteration = 0;
//start loop
//take input
int delta = 13 / Math.pow(2, iteration) + 1;
if(n != 0) {
//if n == 3
else if(n == 2)
{
let -= delta;
}
else if(n == 1)
{
let += delta;
}
}
iteration++;
//end loop
and replace the comments with the code you are already using.