Home > Software engineering >  Ending while loop with string not working
Ending while loop with string not working

Time:04-02

I have a problem with trying to end this while loop. When it runs, typing "exit" for the name part doesn't seem to register and the code continues as if "exit" was a name typed in. What I intended to happen is for "exit" to be typed in so that the loop can end. Any help? Here is the code for the class:

import java.util.*;
import java.io.*;
public class BankTest {
    public static void main(String args[]) {
        List<BankAccount> bank = new ArrayList<BankAccount>();
        Scanner scan = new Scanner(System.in);
        //enter into list the accounts and end with exit typed in
        String name = "";
        while (name.contains("exit")==false) {
            System.out.println("Please enter the name(exit to stop): ");
            name = scan.nextLine();
            System.out.println("Please enter the deposit: ");
            double balance = scan.nextDouble();
            scan.nextLine();
            BankAccount newAccount = new BankAccount(name, balance);
            bank.add(newAccount);
        }
        //find highest account amount
        double largestMon = bank.get(0).getBalance();
        String largestPer = bank.get(0).getName();
        for (int i=0;i<bank.size();i  ) {
            double compare = bank.get(i).getBalance();
            if (compare>largestMon) {
                largestMon = compare;
                largestPer = bank.get(i).getName();
            }
        }
        System.out.println(largestPer " has the largest account with $");
    }
}

I've already tried switching between != and equals() compare the strings.

CodePudding user response:

Change while (name.contains("exit")==false) to while(true)

and add line if (name.equals('exit')) break; after name = scan.nextLine();

CodePudding user response:

First of all you could change name.contains("exit")==falseinto !name.contains("exit")
A better solution would be to use a while true loop and break if the name variable is equal to "exit"

while (true) {
   System.out.println("Please enter the name(exit to stop): ");
   name = scan.nextLine();
   if(name.equals("exit"))
      break;

Notice that in cases like this you shouldn't use .contains() or .equals but rather .equalsIgnoreCase()

CodePudding user response:

This should work

while(true){
        System.out.println("Please enter the name(exit to stop): ");
        name = scan.nextLine();

        if(name.equals("exit"))
            break;

        System.out.println("Please enter the deposit: ");
        double balance = scan.nextDouble();
        scan.nextLine();
        BankAccount newAccount = new BankAccount(name, balance);
        bank.add(newAccount);
}

CodePudding user response:

Modify your while loop as follows:

                while (true) {
                System.out.println("Please enter the name(exit to stop): ");
                name = scan.nextLine();
                if(name.equals("exit"))
                break;
                System.out.println("Please enter the deposit: ");
                double balance = scan.nextDouble();
                scan.nextLine();
                BankAccount newAccount = new BankAccount(name, balance);
                bank.add(newAccount);
            }
  • Related