-2

Hi here is just a fun number guesser where someone inputs a number and my while loop will run until it hits the number inputed. Its just a while loop practice because I am relatively new to Java and wanted more practice. Here is my code. (I am using IntelliJ IDEA)

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        boolean password = false;
        int passwordGuess = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a Password to be guessed");
        int Password = sc.nextInt();
        while (password) {
            System.out.println("Test");
            if (passwordGuess == Password){
                System.out.println("Your Password is " + passwordGuess);
                password = true;
            }
            else{
                System.out.println(passwordGuess);
                passwordGuess++;
            }
        }
    }
}

It prints Enter a Password to be guessed, then I input a number and it prints "Process finished with exit code 0". Any Ideas, Thanks

Stefan Freitag
  • 3,453
  • 3
  • 27
  • 32
E. Seng
  • 23
  • 6
    What do you think the line ``while (password)`` does? – f1sh Aug 18 '17 at 18:28
  • Going off of what f1sh mentioned, look at the value you have for `password`. If you know what `while(password)` does, then you will understand why it isn't going into the loop. – Lexi Aug 18 '17 at 18:29
  • I'm befuddled as to how this doesn't relate to programming as detailed in the the help center. It is a specific programming problem, It describes the specific error problem, and is short enough to be considered a non-optimally contained example. If this is off topic, then what is on topic? – Edwin Buck Aug 18 '17 at 18:41
  • @EdwinBuck This question would have been answered with two minutes in a debugger, and really comes down to a simple mistake. This question is therefore not useful to other users. – Joe C Aug 18 '17 at 19:03
  • @JoeC Better is a judgmental call. I agree that a debugger could have found the issue; but, I also agree that debuggers in the early stages of learning how to program can be just as much of a detriment than a benefit. They can lend a person read their code less and rationalize about it less, while observing the run time more. I've been away from SO a bit, and coming back it seems less than I left. Too much judgement "grooms" kindness (and usefulness) out of the process. This is not a top-shelf question, but it is a programming question, with a short example. SO is not just for experts. – Edwin Buck Aug 19 '17 at 13:05
  • @JoeC And please, keep in mind that I bear no ill will or grudges. I'm just not used to what passes as not-SO-worthy these days. It certainly seems to be difficult to ask a question that doesn't get closed. When they all get closed upon asking, SO will be useless. – Edwin Buck Aug 19 '17 at 13:07
  • @EdwinBuck As ever, if you haven't already, feel free to start a discussion on [meta]. – Joe C Aug 19 '17 at 20:38

6 Answers6

3

You have initialized your password variable as false:

boolean password = false;

and then you try to use it in your while

while (password){/*...*/}

It won't work if password is false.

Juan Carlos Mendoza
  • 5,558
  • 7
  • 24
  • 50
2

You have a variable password which you set to false.

You never set it to true, so your while loop reads "while (false)" which means it exits before running the block.

Edwin Buck
  • 67,527
  • 7
  • 97
  • 130
2

I think I must point to the fact that integer casting to boolean is false for any value. Check this question and answer.

So you might try the solution as orhtej2 posted

while (!password)
Anddo
  • 1,751
  • 1
  • 12
  • 30
1

while loop only loops if expression is true, and you initialize password as false. Hance your loop should read:

while (!password)
orhtej2
  • 1,888
  • 3
  • 18
  • 21
0

set 'boolean password = true' if you want your while loop to start.

OLIVER.KOO
  • 5,276
  • 2
  • 26
  • 53
0

while(false) won't start and currently your password is set to false.set boolean password = true if you want your while loop to start.

OLIVER.KOO
  • 5,276
  • 2
  • 26
  • 53