OK! I am trying to get the program to recognize the letter "E" to exit the while loop. Print my ending statements and then the program is done! Over! That's it! To test this I am just trying to exit as soon as the prompt asks me to put a letter, but no matter what it wont recognize that or any letter I switch it to (that's not D or W) I have tried
I've tried so much I don't remember. Some of these 'solution' ended up looping the questions forever no matter my input. I'll press E, get my own error, press E again and it loops my error. God I need help please.
I just need to press E , Print the not loop statements, and then it exits the program. I'm putting the WHOLE Code down for full analysis please. I am not advanced with Java this is for an Intro to Java Class, I really just know the basics
import java.util.Scanner ;
import java.io.* ;
public class IOExam {
public static void main(String[] args) throws IOException
{
double Deposit = 0;
boolean DepStop = false;
int DEPTransactionNUM = 0;
double DepTotal = 0;
double Withdrawal = 0;
boolean WithStop = false;
int WITHTransactionNUM = 0;
double WithTotal = 0;
Scanner keyboard = new Scanner(System.in);
//Ask For choice
String DepORWith = "" ;
while (!DepORWith.equals("E"))
{
System.out.print("Deposit or Withdrawal D/W ? (Press E to Exit):") ;
DepORWith = keyboard.nextLine() ;
if (DepORWith.equalsIgnoreCase("D"))
{
// Create Document First
PrintWriter DepositTXT = new PrintWriter ("Deposit.txt");
DepositTXT.println("Transaction Number \t Amount");
DepositTXT.println("--------------------------------------");
//Input Deposit
while (DepStop == false)
{
DEPTransactionNUM += 1;
System.out.print("Input amount for deposits:") ;
Deposit = keyboard.nextDouble() ;
keyboard.nextLine() ;
if (Deposit < 0)
{
System.out.print("---ERRROR ---\nInput NON NEGATIVE amount for deposits:") ;
Deposit = keyboard.nextDouble() ;
keyboard.nextLine() ;
}
DepTotal = DepTotal + Deposit;
DepositTXT.printf("\n\t%d \t\t\t $%,.2f", DEPTransactionNUM, Deposit);
String Confirmation ;
System.out.print("Would you Like to deposit again? Y/N: ");
Confirmation = keyboard.nextLine() ;
if (Confirmation.equalsIgnoreCase("y"))
{
DepStop = false;
}
else if (Confirmation.equalsIgnoreCase("n"))
{
DepStop = true;
}
else
{
System.out.println("---ERRROR ---\nINPUT Y OR N\nWould you Like to deposit again? Y/N") ;
Confirmation = keyboard.nextLine() ;
}
}
DepositTXT.println("\n--------------------------------------");
DepositTXT.printf("\nTotal \t\t\t $%,.2f", DepTotal);
DepositTXT.close();
}
else if (DepORWith.equalsIgnoreCase("W"))
{
// Create Document First
PrintWriter WithdrawalTXT = new PrintWriter ("Withdrawal.txt");
WithdrawalTXT.println("Transaction Number \t Amount");
WithdrawalTXT.println("--------------------------------------");
//Input Withdrawals
while (WithStop == false)
{
WITHTransactionNUM += 1;
System.out.print("Input amount for withdrawal:") ;
Withdrawal = keyboard.nextDouble() ;
keyboard.nextLine() ;
if (Withdrawal < 0)
{
System.out.print("---ERRROR ---\nInput NON NEGATIVE amount for withdrawal:") ;
Withdrawal = keyboard.nextDouble() ;
keyboard.nextLine() ;
}
WithTotal = WithTotal + Withdrawal;
WithdrawalTXT.printf("\n\t%d \t\t\t $%,.2f", WITHTransactionNUM, Withdrawal);
String Confirmation ;
System.out.print("Would you Like to withdrawal again? Y/N: ");
Confirmation = keyboard.nextLine() ;
if (Confirmation.equalsIgnoreCase("y"))
{
WithStop = false;
}
else if (Confirmation.equalsIgnoreCase("n"))
{
WithStop = true;
System.out.print("Deposit or Withdrawal D/W ? (Press E to Exit):") ;
DepORWith = keyboard.nextLine() ;
}
else
{
System.out.println("---ERRROR ---\nINPUT Y OR N\nWould you Like to withdrawal again? Y/N: ") ;
Confirmation = keyboard.nextLine() ;
}
}
WithdrawalTXT.println("\n--------------------------------------");
WithdrawalTXT.printf("\nTotal \t\t\t $%,.2f", DepTotal);
WithdrawalTXT.close();
}
else
{
System.out.print("---ERRROR ---\nINPUT D OR W OR E \n") ;
System.out.print("Deposit or Withdrawal D/W ?:") ;
DepORWith = keyboard.nextLine() ;
}
}
System.out.printf("Deposit Total: %,.2d \nWithdrawal Total: %,.2d",DepTotal, WithTotal) ;
System.out.print("\n----------------------------------") ;
System.out.print("\nThank you for Using Old National") ;
System.out.print("\n----------------------------------") ;
}
}
E
or e
do...while
to avoid using DepORWith = keyboard.nextLine();
twice.Do it as follows:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Program {
public static void main(String[] args) throws IOException {
double Deposit = 0;
boolean DepStop = false;
int DEPTransactionNUM = 0;
double DepTotal = 0;
double Withdrawal = 0;
boolean WithStop = false;
int WITHTransactionNUM = 0;
double WithTotal = 0;
Scanner keyboard = new Scanner(System.in);
// Ask For choice
String DepORWith = "";
do {
System.out.print("Deposit or Withdrawal D/W ? (Press E to Exit):");
DepORWith = keyboard.nextLine();
if (!(DepORWith.equalsIgnoreCase("D") || DepORWith.equalsIgnoreCase("W"))) {
if (!DepORWith.equalsIgnoreCase("E")) {
System.out.println("This is not a valid input");
} else {
System.out.println("Goodbye!");
}
} else if (!DepORWith.equalsIgnoreCase("E")) {
if (DepORWith.equalsIgnoreCase("D")) {
// Create Document First
PrintWriter DepositTXT = new PrintWriter("Deposit.txt");
DepositTXT.println("Transaction Number \t Amount");
DepositTXT.println("--------------------------------------");
// Input Deposit
while (DepStop == false) {
DEPTransactionNUM += 1;
System.out.print("Input amount for deposits:");
Deposit = keyboard.nextDouble();
keyboard.nextLine();
if (Deposit < 0) {
System.out.print("---ERRROR ---\nInput NON NEGATIVE amount for deposits:");
Deposit = keyboard.nextDouble();
keyboard.nextLine();
}
DepTotal = DepTotal + Deposit;
DepositTXT.printf("\n\t%d \t\t\t $%,.2f", DEPTransactionNUM, Deposit);
String Confirmation;
System.out.print("Would you Like to deposit again? Y/N: ");
Confirmation = keyboard.nextLine();
if (Confirmation.equalsIgnoreCase("y")) {
DepStop = false;
} else if (Confirmation.equalsIgnoreCase("n")) {
DepStop = true;
} else {
System.out.println("---ERRROR ---\nINPUT Y OR N\nWould you Like to deposit again? Y/N");
Confirmation = keyboard.nextLine();
}
}
DepositTXT.println("\n--------------------------------------");
DepositTXT.printf("\nTotal \t\t\t $%,.2f", DepTotal);
DepositTXT.close();
} else if (DepORWith.equalsIgnoreCase("W")) {
// Create Document First
PrintWriter WithdrawalTXT = new PrintWriter("Withdrawal.txt");
WithdrawalTXT.println("Transaction Number \t Amount");
WithdrawalTXT.println("--------------------------------------");
// Input Withdrawals
while (WithStop == false) {
WITHTransactionNUM += 1;
System.out.print("Input amount for withdrawal:");
Withdrawal = keyboard.nextDouble();
keyboard.nextLine();
if (Withdrawal < 0) {
System.out.print("---ERRROR ---\nInput NON NEGATIVE amount for withdrawal:");
Withdrawal = keyboard.nextDouble();
keyboard.nextLine();
}
WithTotal = WithTotal + Withdrawal;
WithdrawalTXT.printf("\n\t%d \t\t\t $%,.2f", WITHTransactionNUM, Withdrawal);
String Confirmation;
System.out.print("Would you Like to withdrawal again? Y/N: ");
Confirmation = keyboard.nextLine();
if (Confirmation.equalsIgnoreCase("y")) {
WithStop = false;
} else if (Confirmation.equalsIgnoreCase("n")) {
WithStop = true;
System.out.print("Deposit or Withdrawal D/W ? (Press E to Exit):");
DepORWith = keyboard.nextLine();
} else {
System.out
.println("---ERRROR ---\nINPUT Y OR N\nWould you Like to withdrawal again? Y/N: ");
Confirmation = keyboard.nextLine();
}
}
WithdrawalTXT.println("\n--------------------------------------");
WithdrawalTXT.printf("\nTotal \t\t\t $%,.2f", DepTotal);
WithdrawalTXT.close();
}
System.out.printf("Deposit Total: %,.2d \nWithdrawal Total: %,.2d", DepTotal, WithTotal);
System.out.print("\n----------------------------------");
System.out.print("\nThank you for Using Old National");
System.out.print("\n----------------------------------");
} else {
System.out.println("Goodbye!");
}
} while (!DepORWith.equalsIgnoreCase("E"));
}
}
A sample run:
Deposit or Withdrawal D/W ? (Press E to Exit):e
Goodbye!
Another sample run:
Deposit or Withdrawal D/W ? (Press E to Exit):x
This is not a valid input
Deposit or Withdrawal D/W ? (Press E to Exit):e
Goodbye!
Another sample run:
Deposit or Withdrawal D/W ? (Press E to Exit):d
Input amount for deposits:
Another sample run:
Deposit or Withdrawal D/W ? (Press E to Exit):x
This is not a valid input
Deposit or Withdrawal D/W ? (Press E to Exit):d
Input amount for deposits:
Also, change the variable names in the code to follow Java naming conventions e.g. double Deposit
should be double deposit
.