Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6716375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:43:34+00:00 2026-05-26T08:43:34+00:00

I cannot figure this problem out. Everything in the program works the way i

  • 0

I cannot figure this problem out. Everything in the program works the way i want it to except after i use the ‘o’ open account option, ‘w’ withdrawal accout options.. etc that after completion, repeats the loop as desired, but returns the else line “Command was not recognized; please try again.” and then reprints the menu. If i use the ‘s’ command it works fine. i removed the “balance = input.nextDouble();” line and it works correctly, but i can’t figure out why it returns the else statement and reprints.

import java.util.Scanner;
import java.text.*;

public class Bank
{
     public static void main( String[] args )
     {
         Scanner input = new Scanner( System.in );
            String eol = System.getProperty("line.separator");

            DecimalFormat df = new DecimalFormat("0.00");

            int numAccounts = 1;
            String number = "None Selected";
            String userInput;
            double balance = 0;
            double amount = 0;
            int i = 0;
            int j;

            BankAccount[] accounts = new BankAccount [100];

            accounts[0] = new BankAccount (number, balance); 

            String BBalance = df.format(accounts[i].getBalance());

            while (1 < 2){

                if (numAccounts == accounts.length){
                    BankAccount[] tempArray = new BankAccount[accounts.length * 2];
                            for (int k = 0; k < accounts.length; k++){
                                tempArray[k] = accounts[k];
                                }
                                accounts = tempArray;

                }
                try{
                    System.out.print ( eol +
                                    "-----------------------------------------------------" + eol +
                                    "|Commands: o - Open account        c - Close account|" + eol +
                                    "|          d - Deposit             w - Withdraw     |" + eol +
                                    "|          s - Select account      q - Quit         |" + eol +
                                    "-----------------------------------------------------" + eol +
                                    "Current account: " + accounts[i].getNumber() + "       Balance: $" + df.format(accounts[i].getBalance()) +
                                    eol );
                    }
                catch(java.lang.Throwable t) {
                System.out.println("Account number was not found");
                }

                userInput = input.nextLine().trim().toLowerCase();


                if (userInput.substring(0).equals("o")) {
                    System.out.print("Enter Account Number: ");
                    number = input.nextLine();
                    System.out.print("Enter Initial Balance: ");
                    balance = input.nextDouble();

                    accounts[numAccounts++] = new BankAccount (number, balance); 
                    i = numAccounts - 1;
                    continue;
                }
                else if (userInput.substring(0).equals("d")) {
                    System.out.print("Enter Account Number: ");
                    number = input.nextLine();
                        for (i =0; i < numAccounts; i++)
                            if (accounts[i].getNumber().equals(number))
                                break;


                    System.out.print("Enter Amount To Deposit: ");
                    amount = input.nextDouble();

                    accounts[i].deposit(amount);
                    continue;
                }
                else if (userInput.substring(0).equals("s")) {
                    System.out.print("Enter Account Number: ");
                    number = input.nextLine();
                        for (i =0; i < numAccounts; i++){
                            if (accounts[i].getNumber().equals(number)){
                                break;
                            }

                        }
                        if (i != numAccounts){
                            System.out.print("Account number was not found");
                        }
                        continue;
                }
                else if (userInput.substring(0).equals("c")) {
                    System.out.print("Enter Account Number: ");
                    number = input.nextLine();
                        for (i =0; i < numAccounts; i++)
                            if (accounts[i].getNumber().equals(number))
                                break;

                    accounts[i] = accounts[--numAccounts];
                    continue;
                }
                else if (userInput.substring(0).equals("w")) {
                    System.out.print("Enter Account Number: ");
                    number = input.nextLine();
                        for (i =0; i < numAccounts; i++)
                            if (accounts[i].getNumber().equals(number))
                                break;


                    System.out.print("Enter Amount To Withdraw: ");
                    amount = input.nextDouble();

                    accounts[i].withdraw(amount);
                    continue;
                }
                else if (userInput.substring(0).equals("q")) {

                    System.exit(0);
                }
                else{
                    System.out.println("Command was not recognized; please try again.");
                    continue;
                }

            }

            //System.out.print(userInput);
            //System.out.print(accounts[0]);

     }
}

Here’s the BankAccount class:

public class BankAccount {

    String number = "123-456";
    double balance = 1.00; 

    public BankAccount(String accountNumber, double initialBalance){
        number = accountNumber;
        balance = initialBalance;
    }
    public void deposit (double amount){
        balance += amount;
    }
    public void withdraw (double amount){
        balance -= amount;
    }
    public String getNumber(){
        return number;
    }
    public double getBalance(){
        return balance;
    }

    BankAccount[] accounts = new BankAccount [100];

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:43:34+00:00Added an answer on May 26, 2026 at 8:43 am

    You’re not skipping past the end of the lines when you’re reading in the second numbers in the commands that require one.

    Adding an input.nextLine() to those is one quick way around it (non I/O lines redacted).

        // etc.
    } else if (userInput.substring(0).equals("d")) {
        System.out.print("Enter Account Number: ");
        number = input.nextLine();
    
        System.out.print("Enter Amount To Deposit: ");
        amount = input.nextDouble();
        input.nextLine();
    
        continue;
    } // etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this little problem, that I cannot figure out which arguments to pass
After loosing much sleep I still cannot figure this out: The code below (its
I am learning to use polymorphism in C#, but cannot figure out this one.
I have read a ton about this problem, but I cannot figure it out.
I just cannot figure this out, it looks really simple but I'm relatively new
I cannot figure out why I get this error during check-in. I checked in
I cannot figure out how to convert this code from C# to VB.net. It
I know this is probably really obvious, but I cannot figure out why I
I just cannot for the life of me figure out this memory leak in
I have a slight problem and cannot for the life of me figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.