Reputation: 31
Im sure I am missing something simple but I cannot get my do while loop to execute properly. I want it to run through the first time and keep going until the user inputs q. It currently executes once and then loops back up to ask what account to access and then does nothing. Any help or pointing me in the right direction so I can fix it would be greatly appreciated.
public class Crawford_Driver
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double input1;
String accountChoice;
String accountActivity;
RegularAccount regAcct = new RegularAccount(0, .5);
SavingsAccount savAcct = new SavingsAccount(0, .5);
do{
System.out.println("What account would you like to access(regular or savings)?" );
accountChoice = keyboard.nextLine();
if(accountChoice.equalsIgnoreCase("regular"))
System.out.println("What action do you wish to perform(deposit, withdraw or monthly process)? ");
accountActivity = keyboard.nextLine();
if (accountActivity.equalsIgnoreCase("deposit"))
{
System.out.println("How much would you like to deposit?");
input1= keyboard.nextDouble();
regAcct.deposit(input1);
System.out.println("Your balance is " + regAcct.getBalance() );
}
else if (accountActivity.equalsIgnoreCase("withdraw"))
{
System.out.println("How much would you like to withdraw?");
input1= keyboard.nextDouble();
regAcct.withdraw(input1);
System.out.println("Your balance is "+ regAcct.getBalance());
}
else if (accountActivity.equalsIgnoreCase("monthly process"))
{
regAcct.monthlyProcess();
}
else {
if (accountChoice.equalsIgnoreCase("savings"))
if (accountActivity.equalsIgnoreCase("deposit"))
{
System.out.println("How much would you like to deposit?");
input1= keyboard.nextDouble();
savAcct.deposit(input1);
System.out.println("Your balance is " + savAcct.getBalance() );
}
if (accountActivity.equalsIgnoreCase("withdraw"))
System.out.println("How much would you like to withdraw?");
input1= keyboard.nextDouble();
savAcct.withdraw(input1);
System.out.println("Your balance is "+ savAcct.getBalance());
}
}while (!accountChoice.equalsIgnoreCase("Q"));
}
}
Upvotes: 0
Views: 116
Reputation: 3938
You have to make sure you read both options at the start; accountChoice
and accountActivity
. One issue with your code is the lack of {
and }
usage.
So it would be like...
do { // do below first before checking the while condition
if(accountChoice.equalsIgnoreCase("regular"))
{
// do your work.
} else if (accountChoice.equalsIgnoreCase("savings"))
{
// do the rest
}
} while (!accountChoice.equalsIgnoreCase("Q"));
Below is the modified code.
public class Crawford_Driver
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double input1;
String accountChoice;
String accountActivity;
RegularAccount regAcct = new RegularAccount(0, .5);
SavingsAccount savAcct = new SavingsAccount(0, .5);
do {
System.out.println("What account would you like to access(regular or savings)?" );
accountChoice = keyboard.nextLine();
System.out.println("What action do you wish to perform(deposit, withdraw or monthly process)? ");
accountActivity = keyboard.nextLine();
if(accountChoice.equalsIgnoreCase("regular")) { // LINE moved and BRACKET MISSING
if (accountActivity.equalsIgnoreCase("deposit"))
{
System.out.println("How much would you like to deposit?");
input1= keyboard.nextDouble();
regAcct.deposit(input1);
System.out.println("Your balance is " + regAcct.getBalance() );
}
else if (accountActivity.equalsIgnoreCase("withdraw"))
{
System.out.println("How much would you like to withdraw?");
input1= keyboard.nextDouble();
regAcct.withdraw(input1);
System.out.println("Your balance is "+ regAcct.getBalance());
}
else if (accountActivity.equalsIgnoreCase("monthly process"))
{
regAcct.monthlyProcess();
}
}
else if (accountChoice.equalsIgnoreCase("savings"))
{ // line changed & BRACKET MISSING
if (accountActivity.equalsIgnoreCase("deposit"))
{
System.out.println("How much would you like to deposit?");
input1= keyboard.nextDouble();
savAcct.deposit(input1);
System.out.println("Your balance is " + savAcct.getBalance() );
}
else if (accountActivity.equalsIgnoreCase("withdraw"))
{ // bracket missing
System.out.println("How much would you like to withdraw?");
input1= keyboard.nextDouble();
savAcct.withdraw(input1);
System.out.println("Your balance is "+ savAcct.getBalance());
}
}
} while (!accountChoice.equalsIgnoreCase("Q"));
}
}
Upvotes: 0
Reputation: 106498
You're missing a set of curly braces after this statement:
if(accountChoice.equalsIgnoreCase("regular"))
...and this statement:
if (accountActivity.equalsIgnoreCase("withdraw"))
The default behavior for Java (and C, and C++) is to execute only the next line after an if
, for
, or while
statement if curly braces are omitted.
When you're done, your statement should look like this:
if(accountChoice.equalsIgnoreCase("regular")) {
System.out.println("What action do you wish to perform(deposit, withdraw or monthly process)? ");
accountActivity = keyboard.nextLine();
// Rest of code that concerns activity with a "regular" account
}
Upvotes: 1