0

Here's the code:

while (keepGoingDay.equals("y") || keepGoingDay.equals("y")){
        System.out.println(acct1);
        System.out.println(acct2);
        Account.reset();

        while (keepGoing.equals("y") || keepGoing.equals("y"))
            {
            //get account number, what to do, and amount
            System.out.print("\nEnter the number of the account you would like to access: ");
            acctNumber = scan.nextLong();
            System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
            action = scan.next();
            System.out.print("Enter the amount: ");
            amount = scan.nextDouble();

            if (amount > 0)
                if (acctNumber == acct1.getAcctNumber())
                if (action.equals("w") || action.equals("W"))
                    acct1.withdraw(amount);
                else if (action.equals("d") || action.equals("D"))
                    acct1.deposit(amount);
                else 
                    System.out.println("Sorry, invalid action.");
                else if (acctNumber == acct2.getAcctNumber())
                if (action.equals("w") || action.equals("W"))
                    acct1.withdraw(amount);
                else if (action.equals("d") || action.equals("D"))
                    acct1.deposit(amount);
                else 
                    System.out.println("Sorry, invalid action.");
                else
                System.out.println("Sorry, invalid account number.");
            else
                System.out.println("Sorry, amount must be > 0.");


            System.out.print("\nMore transactions? (y/n)");
            keepGoing = scan.next();        
            }
        System.out.println("End of day stats: ");
        System.out.println("Number of deposits: " + Account.getNumDeposits());
        System.out.println("Number of withdrawals: " + Account.getNumWithdrawals());
        System.out.println("Total value of deposits: " + Account.getTotalDeposits());
        System.out.println("Total value of withdrawals: " + Account.getTotalWithdrawals());
        System.out.print("More days?");
        keepGoingDay = scan.next();
         }

}

I don't think the methods are too essential to this so I'll leave them out to save space.

The goal of this program is to have transactions be recorded and counted for multiple days (unknown amount so I couldn't use a for loop).

It goes through the first run fine and after that, it skips over the inner while loop.

I've looked at the braces and don't think that's the issue.

11
  • Well...both of your looping conditions are tautologies... Commented Dec 17, 2012 at 5:47
  • They're not tautologies, they're just redundant (i.e., A or A). Commented Dec 17, 2012 at 5:48
  • I think you mean to check that keepGoing/keepGoingDay are either "y" or "Y". Instead of doing that explicitly, just use keepGoing.equalsIgnoreCase("y"). Commented Dec 17, 2012 at 5:49
  • That's how it was in the skeleton program, so I am not really inclined to change that Commented Dec 17, 2012 at 5:49
  • Wouldn't it be cleaner to do this using a single loop? Commented Dec 17, 2012 at 5:49

3 Answers 3

1

Did you mean Y or y:

while (keepGoing.equals("Y") || keepGoing.equals("y")) 

You code was testing the same thing, ie y, twice.


FYI, your tests could be simplified to:

while (keepGoing.equalsIgnoreCase("y")) 
Sign up to request clarification or add additional context in comments.

1 Comment

As I mentioned above, it's an issue with the skeleton program I was given (was given the actual file, I didn't need to transcribe it) so I'll bring it up with the course teacher tomorrow. Also, that does not fix my loop problem unfortunately.
0

put scan.next(); after your acctNumber = scan.nextLong(); line and amount = scan.nextDouble(); line.

Like this. check my newly added lines.

            System.out.print("\nEnter the number of the account you would like to access: ");
            acctNumber = scan.nextLong();
            scan.next(); // newly added
            System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
            action = scan.next();
            System.out.print("Enter the amount: ");
            amount = scan.nextDouble();
            scan.next(); // newly added

2 Comments

I don't get it, what does that do?
nextLong() and nextDouble only read the long and double value. So when you continue reading with next you receive the "\n" Enter key. So to skip this you have to add the next().
0

If I had the flexibility to change the skeleton program, I would have used boolean variables.

However, the solution that you have to put in place is to reset keepGoing to "y" because it was set to "n" causing it to be skipped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.