Shu
Shu

Reputation: 3

Java Do and While validation

I created this simple program where there is a password, for some reason if I input password the program will carry on executing even though I used do and while.

import java.util.Scanner;

public class PassCheck {
  double password;
  int UserInp;

  public void Validation() {
     do{ 
         System.out.println("Please enter the password");
         Scanner in = new Scanner(System.in);
         int password = in.nextInt();
     } while (password == 1111);

     System.out.println("Please select whether you would like to workout area or perimeter");
     System.out.println("Enter 1 for area and Enter 2 for perimeter");
     Scanner in = new Scanner(System.in);
     int UserInp = in.nextInt();

     switch (UserInp){
        case 1:
            CircleArea CircleAreaObject = new CircleArea();
                CircleAreaObject.area();
        case 2:
     }
  }
}

Upvotes: 0

Views: 104

Answers (3)

Will Byrne
Will Byrne

Reputation: 731

What you have done is to create a new variable password of type int in the scope of your do block, java let you do this because the pacakage-private password field declared at the beginning of your class is of type double rather than int like its's local counterpart. You can correct this problem by making the following changes to your class. Your class also had a lot of style and naming issues which I've cleaned up.

import java.util.Scanner;

public class PassCheck {
  int password;
  int userInp;

  public void validation() {
    do { 
      System.out.println("Please enter the password");
      Scanner in = new Scanner(System.in);
      password = in.nextInt();
    } while (password != 1111);

    System.out.println("Please select whether you would like to workout area or perimeter");
    System.out.println("Enter 1 for area and Enter 2 for perimeter");
    Scanner in = new Scanner(System.in);
    int userInp = in.nextInt();

    switch (userInp){
      case 1:
          CircleArea circleAreaObject = new CircleArea();
          circleAreaObject.area();
      case 2:
    }
  }
}

Upvotes: 0

Summer M
Summer M

Reputation: 11

You have two password variables. The "while" condition is using the method variable. You have another variable declared in the scope inside the while condition-- that one is not the one that the "while" condition is using.

So one solution is to pull out your "int password" declaration into a line immediately before the do/while loop. Then assign it inside the do/while loop scope. And remove the PassCheck password declaration in the class.

Upvotes: 1

Florian Schaetz
Florian Schaetz

Reputation: 10652

Your int password that you actually assign is only valid inside the brackets of your do { ... } block. In the while, you are referencing the double password which is never 1111.

do{ 
    System.out.println("Please enter the password");
    Scanner in = new Scanner(System.in);
    int password = in.nextInt();  // This "password"...
} while (password == 1111); // is NOT the same variable as this...

In the while(password = 1111) you are referencing the double password (as someone already mentioned, why is this double? ). You cannot reference the local int password here, since it's scope is limited to the insides of the brackets above.

Upvotes: 0

Related Questions