Java error message "Cannot be resolved to a variable"?
Solution 1
this variable define after
long n12 = (int) (upc%10);
upc /= 10;
long n11 = (int) (upc%10);
upc /= 10;
long n10 = (int) (upc%10);
upc /= 10;
long n9 = (int) (upc%10);
upc /= 10;
long n8 = (int) (upc%10);
upc /= 10;
long n7 = (int) (upc%10);
upc /= 10;
long n6 = (int) (upc%10);
upc /= 10;
long n5 = (int) (upc%10);
upc /= 10;
long n4 = (int) (upc%10);
upc /= 10;
long n3 = (int) (upc%10);
upc /= 10;
long n2 = (int) (upc%10);
upc /= 10;
long n1 = (int) (upc%10);
int m = (n2 + n4 + n6 + n8 + n10);
long n = (n1 + n3 + n5 + n7 + n9 + n11);
long r = (10-(m+3*n)%10);
you'r using that variable which was not been defined and after using you define that variables
Edit : for if
As you define you method with the return value as long time and you return the String value, see the return value
return (upc + " is a feasible UPC code");
you need to change the return type either in method or as return time like if you want to return this then you method signature looks like this
public long getUpc(){
// and return will work
return (upc + " is a feasible UPC code");
}
but if you want to only numeric value then do not change it's method signature just return the upc like this
return upc;
Solution 2
This is because you haven't declared those variables before you use them on this these lines:
int m = (n2 + n4 + n6 + n8 + n10);
long n = (n1 + n3 + n5 + n7 + n9 + n11);
You declared them after...
Based on what I think you're trying to do, you need to move these three lines to after that large chunk of division/modulus code:
int m = (n2 + n4 + n6 + n8 + n10);
long n = (n1 + n3 + n5 + n7 + n9 + n11);
long r = (10-(m+3*n)%10);
Solution 3
This is because you are using variables before telling the compiler what are they. First you should Declare Them and then use them.
Andrew
Updated on May 30, 2021Comments
-
Andrew about 3 years
ok so I'm kinda new to java and I'm trying to make a class which will be able to ask the user to input a 12 digit long upc code, check to make sure its a valid code, and then display if it is or not. I've got quite a few errors with the current program that I have and I can't seem to figure it out. This is the code that I have so far:
public class Upc { private long upc; public Upc(long upcs) { upc = upcs; } public long getUpc() { int m = (n2 + n4 + n6 + n8 + n10); long n = (n1 + n3 + n5 + n7 + n9 + n11); long r = (10 - (m + 3 * n) % 10); long n12 = (int) (upc % 10); upc /= 10; long n11 = (int) (upc % 10); upc /= 10; long n10 = (int) (upc % 10); upc /= 10; long n9 = (int) (upc % 10); upc /= 10; long n8 = (int) (upc % 10); upc /= 10; long n7 = (int) (upc % 10); upc /= 10; long n6 = (int) (upc % 10); upc /= 10; long n5 = (int) (upc % 10); upc /= 10; long n4 = (int) (upc % 10); upc /= 10; long n3 = (int) (upc % 10); upc /= 10; long n2 = (int) (upc % 10); upc /= 10; long n1 = (int) (upc % 10); if (r == n12) { return (upc + " is a feasible UPC code"); } else { return (upc + " is an invalid UPC code"); } } }
and my errors are as follows:
13 errors found: File: C:\Users\Andrew\Downloads\Upc.java [line: 10] Error: n2 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 10] Error: n4 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 10] Error: n6 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 10] Error: n8 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 10] Error: n10 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 11] Error: n1 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 11] Error: n3 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 11] Error: n5 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 11] Error: n7 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 11] Error: n9 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 11] Error: n11 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 39] Error: Type mismatch: cannot convert from java.lang.String to long File: C:\Users\Andrew\Downloads\Upc.java [line: 42] Error: Type mismatch: cannot convert from java.lang.String to long
I feel like fixing one or two things will eliminate alot of this, can anyone help me?