Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java error message "Cannot be resolved to a variable"?

Tags:

java

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?

like image 439
Andrew Avatar asked Nov 18 '11 04:11

Andrew


People also ask

What does Cannot be resolved mean Java?

"cannot be resolved to a type" means that the compiler has decided that, according to the syntax of the language, what it found at this place in the code has to be type, which means either a class, an interface, or a primitive tpye, but cannot find the definition of any type with that name.

Which of these Cannot be used for a variable name in Java?

Which of these can not be used for a variable name in Java? Explanation: Keywords are specially reserved words which can not be used for naming a user defined variable, example: class, int, for etc.

How do you initialize a variable in Java?

The syntax for an initializer is the type, followed by the variable name, followed by an equal sign, followed by an expression. That expression can be anything, provided it has the same type as the variable. In this case, the expression is 10, which is an int literal.

What does it mean scanner Cannot be resolved to a type?

The error is because Scanner class does not have a constructor taking PrintStream as argument. You should provide InputStream.


3 Answers

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;
like image 109
Pratik Avatar answered Sep 28 '22 16:09

Pratik


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);
like image 25
Mysticial Avatar answered Sep 28 '22 14:09

Mysticial


This is because you are using variables before telling the compiler what are they. First you should Declare Them and then use them.

like image 33
Dark Drake Avatar answered Sep 28 '22 16:09

Dark Drake