Java - Variable Scope

后端 未结 8 1500
心在旅途
心在旅途 2020-12-22 08:42

I\'m brand new to java and I have a super n00bish question. (I do have some general programming knowledge). I\'m trying to access the variable \"item\" to no avail. Can some

相关标签:
8条回答
  • 2020-12-22 09:14

    In Java the variables are local (declared inside a method or inside a constructor) or instance variable (declared inside a class but outside of method). In your case item lives only in the first do while, you can't access it outside the loop. Make item declaration outside the do-while (global variable). I recommend using methods in Java (we are in OOP not in procedural paradigma). Good luck!

    0 讨论(0)
  • 2020-12-22 09:17

    You've declared item in the first do-while loop, you can't use it in the second, because when the control flow exists from the first loop, item goes out of scope.

    Also, as others suggested, proper intentation would have helped you a lot ;-)

    0 讨论(0)
  • 2020-12-22 09:19

    One won't face any problem if he knows the basic java scope rules

    1- The scope of a parameter declaration is the body of the method in which the declaration appears

    2- The scope of a local-variable declaration is from the point at which the declaration appears to the end of that block

    3- The scope of a local-variable declaration that appears in the initialization section of a for statement's header is the body of the for statement and other expressions in the header

    4- A method or field's scope is the entire body of the class

    Be careful about declaring local variables which have the same name as the class instance variables since it would shadow the instance variable and makes confusion about the scope of the variables and program readability

    0 讨论(0)
  • 2020-12-22 09:24

    Declare variable Item outside the do-while loop.

    int exit = 0;
    String item = null;
        do {
        System.out.println("Enter item number: ");
        itm = input.next();
    
    0 讨论(0)
  • 2020-12-22 09:25
    int exit = 0;
    String item = null;
    do {
    System.out.println("Enter item number: ");
    item = input.next();
    if (ValidateItem(item) == true){
        if (Integer.parseInt(item) <=4 && Integer.parseInt(item) >=1){
            exit = 1;}
    else System.out.println("Enter an item number between 1 and 4");
    }
    if (ValidateItem(item) == false){
        System.out.println("Enter an item number between 1 and 4");}
    } while (exit == 0);
    
    int exitQuan = 0;
    do {
     System.out.println("Enter quantity (1-99): ");
     String quant = input.next();
     if (ValidateItem(quant) == true){
                exitQuan = 1;}
        else System.out.println("Enter a quantity between 1 and 99");
    } while (exitQuan == 0);
    
    0 讨论(0)
  • 2020-12-22 09:33

    Variable scope only extends to the smallest pair of braces that surround the declaration. For instance:

    //this could be a method body, an if statement, a loop, whatever
    {
        int x;
    } //x passes out of scope here
    

    Therefore, when you declare item inside of a do-while loop, its scope ends once you exit the loop. To fix this, declare item above the loop like this:

    String item = null; //initialize to null to avoid warning about using an un-initialized variable
    
    do {
        System.out.println("Enter item number: ");
        item = input.next();
    
        //rest of loop...
    
    } while (exit == 0);
    

    This way item will be available until the method returns.

    0 讨论(0)
提交回复
热议问题