Ethan Trevino
Ethan Trevino

Reputation: 23

java while loop getting stuck

So I have a class project for school and I have to make a bank account program, everything runs smoothly until I hit the while loop, and then it gets stuck in an infinite loop, not executing anything, please help, the code is the following

import java.util.Scanner;


public class Blueprint_ET
{
  //initialize all variables in the object
  private double balance;
  private double interestRate;
  private double years;
  private String name;
  private int age;
  private int validAmount;
  private double initialBalance;

  //construct the objects with the following variables
  public Blueprint_ET(double balance, String name, int age){
    years=0;
    interestRate=.02;
    this.balance=balance;
    this.name = name;
    this.age= age;
    initialBalance = balance;
  }

  //deposit method
  public void deposit(double depositAmt){
    balance+=depositAmt;
  }

  //withdraw method
  public void withdraw(double withdrawAmt){
    balance-=withdrawAmt;
  }

  //method used to set the account years and add the interest accordingly
  public void setYearsAndAddInterest(double years){
    this.years=years;
    double interestEarned = (interestRate * years) * balance;
    balance += interestEarned;
  }

  //method used to get the name so that it can be printed out later on
  public String getName(){
    return name;
  }

  //method used to get the initial balance so that it can be printed out
  public double getStarting(){
    return initialBalance;
  }

  //method used to get the years so that it can be printed out
  public double getYears(){
    return years;
  }

  //method used to get the final balance once the account it closed
  public double getFinal(){
    return balance;
  }

  //method used to get the age of the person
  public int getAge(){
    return age;
  }
  public static void main(String[] args){

    //create "kboard" scanner
    Scanner kboard = new Scanner(System.in);

    //ask the user to enter the persons first name
    System.out.print("Enter the account owners first name here: ");
    String firstName = kboard.nextLine();

    //ask the user to enter the persons last name
    System.out.print("Enter the account owners last name here: ");
    String lastName = kboard.nextLine();

    //puts together the first and last name into one variable
    String fullName = firstName + " " + lastName;

    //asks the user for the starting balance of the account
    System.out.print("Enter the starting balance of the account here: ");
    double balance = kboard.nextDouble();

    //asks the user for the age of the person
    System.out.print("Enter the age of the person here: ");
    int age = kboard.nextInt();

    //initialize variables that will be used in the while loop
    String option;
    int exitNum=0;
    double years=0;
    double deposit=0;
    double withdraw=0;

    //Create account object
    Blueprint_ET account1 = new Blueprint_ET(balance, fullName, age);

    //start while loop
    while (exitNum < 20 ){

      //prompts the user to enter what option they want to do with according codes
      System.out.print("To widthdraw, type wd, to deposit, type d, to change the number of years that the account has been open and add the according interest, type y, to close the account, type c.");
      option = kboard.nextLine();

      //if statement for entering the amount of money withdrawn if the option selected is the code for wd
      if (option == "wd") {
        System.out.print("Enter the amount you want to withdraw from the account: ");
        withdraw = kboard.nextDouble();
        account1.withdraw(withdraw);
      }

      //if statement for entering the years the account has been open and sets the years and adds the interest rate into the balance
      else if (option == "y") {
        System.out.print("Enter the years the person has had the account open: ");
        years = kboard.nextDouble();
        account1.setYearsAndAddInterest(years);
      }

      //if statement for entering the amount of money to deposit, adds it to balance
     else if (option == "d") {
        System.out.print("Enter the amount you want to deposit: ");
        deposit = kboard.nextDouble();
        account1.deposit(deposit);
      }

      //sets the exitNum to 21 so that it can exit the while loop
      else if (option == "e") {
        exitNum = 21;
      }

    } 
    //prints out all data once the account has been closed
    System.out.println(account1.getName()+"'s is "+account1.getAge()+" had an account that had an intial balance of "+account1.getStarting()+". This account was open for "+account1.getYears()+" years. Also, when the account was closed, they had a balance of "+account1.getFinal());
  }
}

Upvotes: 2

Views: 271

Answers (4)

Wojtek
Wojtek

Reputation: 1308

You should always compare two strings with equals() method. It actually compares the expression which is stored by a String object. For reference types operator == compares the addresses of two objects in memory

Upvotes: 0

stackoverflow
stackoverflow

Reputation: 78

You must use a.equals("abc") to compare one string to another. And you have to know the difference between equals() and ==. I have run your code, this is the problem,I think.

Upvotes: 1

Raman Sahasi
Raman Sahasi

Reputation: 31841

Consider this code of yours:

//sets the exitNum to 21 so that it can exit the while loop
else if (option == "e") {
    exitNum = 21;
}

Note that there are 2 ways to compare:

  1. Using equals to == operator.
  2. Using str.equals(str2) method.

  1. Using equals to == operator.

    == tests for reference equality. Let's suppose you have a String str1 = "Hello World". Now this str1 String object takes up some space in memory. Now let's suppose you create another exact same String str2 = "Hello World". Now it's not always known whether these two different Hello World String objects would return true or false when compared with str1 == str2. They may return true if both of these objects are referring to the same Object in memory. However, this may also return false if they refer to different memory locations.

    Bottomline is that == operator tests for the reference. It doesn't matter to this operator as to what's there inside the objects.  
     

  2. Using str.equals(str2) method.

    This str.equals(str2) would actually check what's there inside the String Objects. It doesn't matter to this method whether both the objects refer to the same or different memory locations. It only considers the inside material of the String Objects.


Hence you should always use .equals() method when comparing String Objects. And so your method becomes:

//sets the exitNum to 21 so that it can exit the while loop
else if (option.equals("e")) {
    exitNum = 21;
}

Upvotes: 3

Samuel Kok
Samuel Kok

Reputation: 585

You have used the == comparator, which perform a reference check on the two strings. Since the reference check will never be true, it will end up in an infinite loop, skipping all the if else statements

Since you are comparing the contents in the strings, use equals() instead.

You can look up this answer for a detailed explanation.

Upvotes: 0

Related Questions