James
James

Reputation: 338

Why does this while loop execute infinite times?

I have a while loop which should read through elements of an array to try and find the value of the given variable, however, the loop executes itself infinite times and I can't see why. It should exit once it has found the value it is looking for; I know it does find what it's looking for because it prints out I've found it! infinite times.The code for the method, so far is:

try{
    System.out.println("Enter your card number to access your account:");
    int CardNumber = sc.nextInt();
    String CardNumberStr = Integer.toString(CardNumber);
    boolean Exist = false;
    String LineNo;
    String [] CardNum = {};
    int Counter;
    FileReader fileReader = new FileReader("VirtualATM.txt");
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    line = bufferedReader.readLine();
    CardNum = line.split("\\s+");
    do{
        for(Counter = 0; Counter < CardNum.length; Counter++){
            LineNo = CardNum[Counter];
            if(LineNo.contains(CardNumberStr)){
                Exist = true;
                System.out.println("I've found it!");
            }
            else if(Counter == CardNum.length){
                Exist=false;
            }
        }
    }while(Exist = false || line != null);
    bufferedReader.close();
}catch(FileNotFoundException e){
    e.printStackTrace();
    System.out.println(e.getMessage());
}catch(IOException e){
    e.printStackTrace();
    System.out.println(e.getMessage());
}

Can anyone help me figure out why it does this please?

Upvotes: 1

Views: 180

Answers (6)

Florian F
Florian F

Reputation: 1377

Another problem: in

    for(Counter = 0; Counter < CardNum.length; Counter++){

        LineNo = CardNum[Counter];
        if(LineNo.contains(CardNumberStr)){
            Exist = true;
            System.out.println("I've found it!");
        }
        else if(Counter == CardNum.length){
            Exist=false;
        }
    }

(Counter == CardNum.length) will never be true, since the Count values goes from 0 to CardNum.length-1. Since Exist is initialized to false, you don't need to set it to false again. You can drop the else clause.

And by the way, you can break out of the loop

    for(Counter = 0; Counter < CardNum.length; Counter++){
        LineNo = CardNum[Counter];
        if(LineNo.contains(CardNumberStr)){
            Exist = true;
            System.out.println("I've found it!");
            break.
        }
    }

Upvotes: 2

Avinash Kumar Pandey
Avinash Kumar Pandey

Reputation: 752

Exist = false is the root of all evil here. = is the assignment operator and == is the equality comparison operator.

Upvotes: 2

Nate Birkholz
Nate Birkholz

Reputation: 2809

You are not evaluating Exist against the value false, you are assigning the value false to the variable. It's odd that there isn't more going wrong with the conditional or, there, but you will be able to fix it by setting the line to

      while(Exist == false || line != null);

I also could be wrong as it is late and I am on an iPad, but is that "while" at the right level of the do? It might need to be one curly bracket up.

Upvotes: 1

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Because you're assigning Exist = false in the do-while loop. It should be Exists == false or way better: !Exist:

} while(!Exist || line != null);

Apart of this, please follow the Java Code Conventions (old but still used), where the variables should be declared with camel case but starting with lower case letters.


Reviewing more your code, you're never reading another line of your file and the logic for your do-while should use AND (&&), not OR (||). Just add this to your code:

line = bufferedReader.readLine();
CardNum = line.split("\\s+");
do{
    for(Counter = 0; Counter < CardNum.length; Counter++){
        LineNo = CardNum[Counter];
        if(LineNo.contains(CardNumberStr)){
            Exist = true;
            System.out.println("I've found it!");
        }
        else if(Counter == CardNum.length){
            Exist=false;
        }
    }
    //add this line to read another line of the file
    //and check if it exists
    line = bufferedReader.readLine();
} while(!Exist && line != null);

Upvotes: 6

You're not re-reading your line variable inside your loop, so line != null is always true.

Upvotes: 2

Jens
Jens

Reputation: 69440

Your code is wron in this line:

while(Exist = false || line != null);

It must be:

while(Exist == false || line != null);
            ^^^^

In you version you assign false to Exist and you do not compare is.

Upvotes: 1

Related Questions