Here is my code for my main method. My teacher gave me this remark. Does anyone
ID: 3557971 • Letter: H
Question
Here is my code for my main method. My teacher gave me this remark.
Does anyone know what they are looking for and how to fix it? I marked the area he wants fixed with an arrow.
public static void main(String[] args) throws FileNotFoundException {
ArrayList list =
new ArrayList();
int choice = 0;
do {
while (true) { <----this needs to be fixed I guess
System.out.println("1. Deposit money");
System.out.println("2. Withdraw money");
System.out.println("3. Check balance");
System.out.println("4. Create new account");
System.out.print("Your choice, 0 to quit: ");
try {
choice = Integer.parseInt(
input.nextLine());
break;
}
catch (Exception e) {
System.out.println("Error: Invalid entry! Please Try Again!");
continue;
}
}
switch (choice) {
case 1:
if (list.size() == 0) {
System.out.println("No account exist. Please create a account first");
break;
}
depositMoney(list);
break;
case 2:
if (list.size() == 0) {
System.out.println("No account exist. Please create a account first");
break;
}
withdrawMoney(list);
break;
case 3:
if (list.size() == 0) {
System.out.println("No account exist. Please create a account first");
break;
}
checkBalance(list);
break;
case 4:
createNewAccount(list);
break;
case 0:
System.out.println("Thank you for trusting us with your banking needs!");
break;
default:
System.out.println("Invalid option is selected! Please try again");
}
System.out.println();
}
while (choice != 0);
if (list.size() > 0) {
displayResults(list);
}
}
Explanation / Answer
Change the while(true) to while(choice) as we want the loop to execute only when choice is not equal to zero. So, impose this condition.
public static void main(String[] args) throws FileNotFoundException {
ArrayList list =
new ArrayList();
int choice = 1;
do {
while(choice) {
System.out.println("1. Deposit money");
System.out.println("2. Withdraw money");
System.out.println("3. Check balance");
System.out.println("4. Create new account");
System.out.print("Your choice, 0 to quit: ");
try {
choice = Integer.parseInt(
input.nextLine());
break;
}
catch (Exception e) {
System.out.println("Error: Invalid entry! Please Try Again!");
continue;
}
}
switch (choice) {
case 1:
if (list.size() == 0) {
System.out.println("No account exist. Please create a account first");
break;
}
depositMoney(list);
break;
case 2:
if (list.size() == 0) {
System.out.println("No account exist. Please create a account first");
break;
}
withdrawMoney(list);
break;
case 3:
if (list.size() == 0) {
System.out.println("No account exist. Please create a account first");
break;
}
checkBalance(list);
break;
case 4:
createNewAccount(list);
break;
case 0:
System.out.println("Thank you for trusting us with your banking needs!");
break;
default:
System.out.println("Invalid option is selected! Please try again");
}
System.out.println();
}
while (choice != 0);
if (list.size() > 0) {
displayResults(list);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.