dimanche 31 octobre 2021

If statement not getting executed even though it is true

this if(line[2] == "Inactive") Statement is not executing.

public void Transaction(int test) throws IOException {
        Scanner reader = new Scanner(new FileReader(bankfile));
        String currentline = "";
        try {
        while((currentline = reader.nextLine())!= null) {
            String line[] = currentline.split(",");
            System.out.println(currentline);
            if(Integer.parseInt(line[0]) == test){
                System.out.println(line[2]);
                if(line[2] == "Inactive") { **This is the Problem**
                    System.out.println("This account is Inactive and cannot perform any transaction.");
                    return;
                }
                else {
                    System.out.println("Hello");
                break;
                }
            }
        }
        }
        catch(NoSuchElementException e) {
            System.out.println("Account Number is not found.");
            return;
        }
//      performTrans();
    }

It is executing the else statement even though line[2] == "Inactive"

Here is the file.

File:
2,2.0,Active
1,393.0,Inactive
3,3.0,Active
4,4.0,Active
5,5.0,Active

I want this code

Transaction(1);

to get an output "This account is Inactive and cannot perform any transaction" because account 1 is Inactive. But it is executing the else statement and not the if statement and so the output will be "Hello"

Aucun commentaire:

Enregistrer un commentaire