Java error message “cannot resolve to variable”?

OK, so I have something new about Java. I'm trying to create a class that can ask the user to enter a 12 bit UPC code, check to make sure it is a valid code, and then show whether it is or not There are many mistakes in my current program. I can't seem to understand it This is my code 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");
        }
    }
}

My mistakes 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 think fixing one or two things will eliminate a lot of this. Can someone help me?

Solution

This variable is defined 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 use the undefined variable and define the variable after use

Edit: for if

When you define a method with a return value, if you return a string value, see return value

return (upc + " is a feasible UPC code");

If you need to change the return type on method or return, just as you want to return it, then your method signature is like this

public long getUpc(){
  // and return will work
  return (upc + " is a feasible UPC code"); 
}

But if you only want the value, don't change its method signature, just return UPC like this

return upc;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>