Results 1 to 7 of 7
  1. #1
    cshoya is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default Help with program

    I am struggling through writing this program that I have started. When I compile it to see if I have everything right java tells me that it cannot find the symbol I defined earlier. If anyone could take a look at my program and give me some advice that would be awesome.

    Here it is:
    public class CreditCard {

    private String number; //The Credit Card number is kept as a string
    private boolean valid;

    public CreditCard(String n){
    number=n;
    valid=true;
    }

    private void check2(){
    int v1=Integer.parseInt(number.substring(6,7));
    int v1d = v1*2;
    if(v1d<=9){
    int v1d1 = v1d%10;
    (int) v1d2 = v1d/10;
    }
    else{
    int v1d3 = v1d;
    }

    int v2=Integer.parseInt(number.substring(4,5));
    int v2d = v2*2;
    if(v2d<=9){
    int v2d1 = v2d%10;
    (int) v2d2 = v2d/10;
    }
    else{
    int v2d3 = v2d;
    }

    int v3=Integer.parseInt(number.substring(2,3));
    int v3d =v3*2;
    if(v3d<=9){
    int v3d1 = v3d%10;
    (int) v3d2 = v3d/10;
    }
    else{
    int v3d3 = v3d;
    }

    int v4=Integer.parseInt(number.substring(0,1));
    int v4d = v4*2;
    if(v4d<=9){
    int v4d1 = v4d%10;
    (int) v4d2 = v4d/10;
    }
    else{
    int v4d3 = v4d;
    }

    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 $
    }

    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    It helps in cases like this to copy and post the exact and entire compiler message and indicate which line(s) of your code it is referring to.

  3. #3
    cshoya is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    $ javac CreditCard.java
    CreditCard.java:28: cannot find symbol
    symbol : variable v1d2
    location: class CreditCard
    (int) v1d2 = v1d/10;
    ^
    CreditCard.java:28: unexpected type
    required: variable
    found : value
    (int) v1d2 = v1d/10;
    ^
    CreditCard.java:38: cannot find symbol
    symbol : variable v2d2
    location: class CreditCard
    (int) v2d2 = v2d/10;
    ^
    CreditCard.java:38: unexpected type
    required: variable
    found : value
    (int) v2d2 = v2d/10;
    ^
    CreditCard.java:48: cannot find symbol
    symbol : variable v3d2
    location: class CreditCard
    (int) v3d2 = v3d/10;
    ^
    CreditCard.java:48: unexpected type
    required: variable
    found : value
    (int) v3d2 = v3d/10;
    ^
    CreditCard.java:58: cannot find symbol
    symbol : variable v4d2
    location: class CreditCard
    (int) v4d2 = v4d/10;
    ^
    CreditCard.java:58: unexpected type
    required: variable
    found : value
    (int) v4d2 = v4d/10;
    ^
    CreditCard.java:64: cannot find symbol
    symbol : variable v4d1
    location: class CreditCard
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 + v3d3 + v2d3 + v1d3);
    ^
    CreditCard.java:64: cannot find symbol
    symbol : variable v3d1
    location: class CreditCard
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 + v3d3 + v2d3 + v1d3);
    ^
    CreditCard.java:64: cannot find symbol
    symbol : variable v2d1
    location: class CreditCard
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 + v3d3 + v2d3 + v1d3);
    ^
    CreditCard.java:64: cannot find symbol
    symbol : variable v1d1
    location: class CreditCard
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 + v3d3 + v2d3 + v1d3);
    ^
    CreditCard.java:64: cannot find symbol
    symbol : variable v4d2
    location: class CreditCard
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 + v3d3 + v2d3 + v1d3);
    ^
    CreditCard.java:64: cannot find symbol
    symbol : variable v3d2
    location: class CreditCard
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 + v3d3 + v2d3 + v1d3);
    ^
    CreditCard.java:64: cannot find symbol
    symbol : variable v2d2
    location: class CreditCard
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 + v3d3 + v2d3 + v1d3);
    ^
    CreditCard.java:64: cannot find symbol
    symbol : variable v1d2
    location: class CreditCard
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 + v3d3 + v2d3 + v1d3);
    ^
    CreditCard.java:64: cannot find symbol
    symbol : variable v4d3
    location: class CreditCard
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 + v3d3 + v2d3 + v1d3);
    ^
    CreditCard.java:64: cannot find symbol
    symbol : variable v3d3
    location: class CreditCard
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 + v3d3 + v2d3 + v1d3);
    ^
    CreditCard.java:64: cannot find symbol
    symbol : variable v2d3
    location: class CreditCard
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 + v3d3 + v2d3 + v1d3);
    ^
    CreditCard.java:64: cannot find symbol
    symbol : variable v1d3
    location: class CreditCard
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 + v3d3 + v2d3 + v1d3);
    ^
    CreditCard.java:64: incompatible types
    found : java.lang.String
    required: int
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 + v3d3 + v2d3 + v1d3);
    ^
    21 errors

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3
    How about using meaningful variable names?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    int c1 = (v4d1 + v3d1 + v2d1 + v1d1 + v4d2 + v3d2 + v2d2 + v1d2 + v4d3 $
    I guess there's a typo here. But a more fundemental problem exists with the variables v4d1 and the rest.

    In general variables have a scope. The scope refers to where in your code you can legally refer to a variable you have declared. (You can't refer to it everywhere; a fact which allows for variables to be reused.) The rule in Java is that once you declare a variable it remains in scope until the } which matches the { of the block where the variable was declared. An example might be clearer:

    Java Code:
    void someMethod() {
        if(someCondition) { // the "starting" { of foo's block
            int foo = 666;
            if(foo < 10) {
                // some code here
            }
            foo++;
        } // the ending } of the block.  foo is not in scope beyound this point.
        System.out.println(foo); // no! the compiler will not know what the variable foo is supposed to be
    }
    Check that your variables are "in scope". If not you may have to declare them earlier in your code.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You have a multitude of scope issues. Variables declared inside a code block are not visible outside that block. Methods, loops, if statements are all examples of code blocks, Basically anything surrounded by braces { }. You can also have an independent code block.
    Java Code:
    { // start of block
        String text = "hello";
    }
    Sytem.out.println(text); // error

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    How about using meaningful variable names?
    Agree. Also it's a good idea to get into the habit of documenting what a method actually does. In this case it could probably be done in just a single sentence.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 01:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 07:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •