JAVA While Loop not recognizing String.equals() in Condition.

huangapple go评论65阅读模式
英文:

JAVA While Loop not recignizing String.eqauls() in Condition

问题

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("---ERROR ---\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("---ERROR ---\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("---ERROR ---\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("---ERROR ---\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("---ERROR ---\nINPUT D OR W OR E \n");
			   System.out.print("Deposit or Withdrawal D/W ?:");
			   DepORWith = keyboard.nextLine(); 
		   }
	   }
	   System.out.printf("Deposit Total: %,.2f \nWithdrawal Total: %,.2f", DepTotal, WithTotal);
	   System.out.print("\n----------------------------------");
	   System.out.print("\nThank you for Using Old National");
	   System.out.print("\n----------------------------------");
   }   
}
英文:

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

  • toUpperCase(), both after input and with the nextLine() method
  • while (!DepORWith.equals("E") || !DepORWith.equals("e"))
  • while (!DepORWith.equals("E") && !DepORWith.equals("e"))
  • Switching the cases ever which way
  • Trying to change it all to char types (BIG mistake)
  • putting a letter as a place holder and then it'll just change
  • Using .equalsIgnoreCase() with these other 'solutions'
  • Using .isEmpty()

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 = &quot;&quot; ; 
while (!DepORWith.equals(&quot;E&quot;))
{
System.out.print(&quot;Deposit or Withdrawal D/W ? (Press E to Exit):&quot;) ;
DepORWith = keyboard.nextLine() ;  
if (DepORWith.equalsIgnoreCase(&quot;D&quot;))
{
// Create Document First 
PrintWriter DepositTXT = new PrintWriter (&quot;Deposit.txt&quot;);
DepositTXT.println(&quot;Transaction Number \t Amount&quot;);
DepositTXT.println(&quot;--------------------------------------&quot;);
//Input Deposit 
while (DepStop == false)
{
DEPTransactionNUM += 1;
System.out.print(&quot;Input amount for deposits:&quot;) ;
Deposit = keyboard.nextDouble() ; 
keyboard.nextLine() ; 
if (Deposit &lt; 0)
{
System.out.print(&quot;---ERRROR ---\nInput NON NEGATIVE amount for deposits:&quot;) ;
Deposit = keyboard.nextDouble() ;
keyboard.nextLine() ; 
}
DepTotal = DepTotal + Deposit;
DepositTXT.printf(&quot;\n\t%d \t\t\t $%,.2f&quot;, DEPTransactionNUM, Deposit);
String Confirmation ;
System.out.print(&quot;Would you Like to deposit again? Y/N: &quot;);
Confirmation = keyboard.nextLine() ;
if (Confirmation.equalsIgnoreCase(&quot;y&quot;))
{
DepStop = false;
}
else if (Confirmation.equalsIgnoreCase(&quot;n&quot;))
{
DepStop = true; 
}
else 
{
System.out.println(&quot;---ERRROR ---\nINPUT Y OR N\nWould you Like to deposit again? Y/N&quot;) ;
Confirmation = keyboard.nextLine() ;
}
}
DepositTXT.println(&quot;\n--------------------------------------&quot;);
DepositTXT.printf(&quot;\nTotal \t\t\t $%,.2f&quot;, DepTotal);
DepositTXT.close();
}
else if (DepORWith.equalsIgnoreCase(&quot;W&quot;))
{
// Create Document First 
PrintWriter WithdrawalTXT = new PrintWriter (&quot;Withdrawal.txt&quot;);
WithdrawalTXT.println(&quot;Transaction Number \t Amount&quot;);
WithdrawalTXT.println(&quot;--------------------------------------&quot;);
//Input Withdrawals 
while (WithStop == false)
{
WITHTransactionNUM += 1;
System.out.print(&quot;Input amount for withdrawal:&quot;) ;
Withdrawal = keyboard.nextDouble() ; 
keyboard.nextLine() ; 
if (Withdrawal &lt; 0)
{
System.out.print(&quot;---ERRROR ---\nInput NON NEGATIVE amount for withdrawal:&quot;) ;
Withdrawal = keyboard.nextDouble() ;
keyboard.nextLine() ; 
}
WithTotal = WithTotal + Withdrawal;
WithdrawalTXT.printf(&quot;\n\t%d \t\t\t $%,.2f&quot;, WITHTransactionNUM, Withdrawal);
String Confirmation ;
System.out.print(&quot;Would you Like to withdrawal again? Y/N: &quot;);
Confirmation = keyboard.nextLine() ;
if (Confirmation.equalsIgnoreCase(&quot;y&quot;))
{
WithStop = false;
}
else if (Confirmation.equalsIgnoreCase(&quot;n&quot;))
{
WithStop = true;
System.out.print(&quot;Deposit or Withdrawal D/W ? (Press E to Exit):&quot;) ;
DepORWith = keyboard.nextLine() ; 
}
else 
{
System.out.println(&quot;---ERRROR ---\nINPUT Y OR N\nWould you Like to withdrawal again? Y/N: &quot;) ;
Confirmation = keyboard.nextLine() ;
}
}
WithdrawalTXT.println(&quot;\n--------------------------------------&quot;);
WithdrawalTXT.printf(&quot;\nTotal \t\t\t $%,.2f&quot;, DepTotal);
WithdrawalTXT.close();
}
else 
{
System.out.print(&quot;---ERRROR ---\nINPUT D OR W OR E \n&quot;) ;
System.out.print(&quot;Deposit or Withdrawal D/W ?:&quot;) ;
DepORWith = keyboard.nextLine() ; 
}
}
System.out.printf(&quot;Deposit Total: %,.2d \nWithdrawal Total: %,.2d&quot;,DepTotal, WithTotal) ;
System.out.print(&quot;\n----------------------------------&quot;) ;
System.out.print(&quot;\nThank you for Using Old National&quot;) ;
System.out.print(&quot;\n----------------------------------&quot;) ;
}
}

答案1

得分: 0

!DepORWith.equals("E") || !DepORWith.equals("e") 将始终返回 true。如果 DepORWith"E",它不是 "e",反之亦然。从逻辑上讲,您需要一个 && 条件:

while (!DepORWith.equals("E") && !DepORWith.equals("e")) {

尽管在这种情况下,您可以稍微优化代码并使用 equalsIgnoreCase

while (!DepORWith.equalsIgnoreCase("E")) {
英文:

!DepORWith.equals(&quot;E&quot;) || !DepORWith.equals(&quot;e&quot;) will always return true. If DepORWith is &quot;E&quot; it's not e and vise-versa. Logically, you need an `&& condition:

while (!DepORWith.equals(&quot;E&quot;) &amp;&amp; !DepORWith.equals(&quot;e&quot;)) {

Although in this case, you could clean up the code a bit and use equalsIgnoreCase:

while (!DepORWith.equalsIgnoreCase(&quot;E&quot;)) {

答案2

得分: 0

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) {
                        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) {
                        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"));
    }
}

Please let me know if you have any further questions or if there's anything else I can assist you with.

英文:
  1. Do the required processing when the input is not E or e
  2. You better use 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 = &quot;&quot;;
do {
System.out.print(&quot;Deposit or Withdrawal D/W ? (Press E to Exit):&quot;);
DepORWith = keyboard.nextLine();
if (!(DepORWith.equalsIgnoreCase(&quot;D&quot;) || DepORWith.equalsIgnoreCase(&quot;W&quot;))) {
if (!DepORWith.equalsIgnoreCase(&quot;E&quot;)) {
System.out.println(&quot;This is not a valid input&quot;);
} else {
System.out.println(&quot;Goodbye!&quot;);
}
} else if (!DepORWith.equalsIgnoreCase(&quot;E&quot;)) {
if (DepORWith.equalsIgnoreCase(&quot;D&quot;)) {
// Create Document First
PrintWriter DepositTXT = new PrintWriter(&quot;Deposit.txt&quot;);
DepositTXT.println(&quot;Transaction Number \t Amount&quot;);
DepositTXT.println(&quot;--------------------------------------&quot;);
// Input Deposit
while (DepStop == false) {
DEPTransactionNUM += 1;
System.out.print(&quot;Input amount for deposits:&quot;);
Deposit = keyboard.nextDouble();
keyboard.nextLine();
if (Deposit &lt; 0) {
System.out.print(&quot;---ERRROR ---\nInput NON NEGATIVE amount for deposits:&quot;);
Deposit = keyboard.nextDouble();
keyboard.nextLine();
}
DepTotal = DepTotal + Deposit;
DepositTXT.printf(&quot;\n\t%d \t\t\t $%,.2f&quot;, DEPTransactionNUM, Deposit);
String Confirmation;
System.out.print(&quot;Would you Like to deposit again? Y/N: &quot;);
Confirmation = keyboard.nextLine();
if (Confirmation.equalsIgnoreCase(&quot;y&quot;)) {
DepStop = false;
} else if (Confirmation.equalsIgnoreCase(&quot;n&quot;)) {
DepStop = true;
} else {
System.out.println(&quot;---ERRROR ---\nINPUT Y OR N\nWould you Like to deposit again? Y/N&quot;);
Confirmation = keyboard.nextLine();
}
}
DepositTXT.println(&quot;\n--------------------------------------&quot;);
DepositTXT.printf(&quot;\nTotal \t\t\t $%,.2f&quot;, DepTotal);
DepositTXT.close();
} else if (DepORWith.equalsIgnoreCase(&quot;W&quot;)) {
// Create Document First
PrintWriter WithdrawalTXT = new PrintWriter(&quot;Withdrawal.txt&quot;);
WithdrawalTXT.println(&quot;Transaction Number \t Amount&quot;);
WithdrawalTXT.println(&quot;--------------------------------------&quot;);
// Input Withdrawals
while (WithStop == false) {
WITHTransactionNUM += 1;
System.out.print(&quot;Input amount for withdrawal:&quot;);
Withdrawal = keyboard.nextDouble();
keyboard.nextLine();
if (Withdrawal &lt; 0) {
System.out.print(&quot;---ERRROR ---\nInput NON NEGATIVE amount for withdrawal:&quot;);
Withdrawal = keyboard.nextDouble();
keyboard.nextLine();
}
WithTotal = WithTotal + Withdrawal;
WithdrawalTXT.printf(&quot;\n\t%d \t\t\t $%,.2f&quot;, WITHTransactionNUM, Withdrawal);
String Confirmation;
System.out.print(&quot;Would you Like to withdrawal again? Y/N: &quot;);
Confirmation = keyboard.nextLine();
if (Confirmation.equalsIgnoreCase(&quot;y&quot;)) {
WithStop = false;
} else if (Confirmation.equalsIgnoreCase(&quot;n&quot;)) {
WithStop = true;
System.out.print(&quot;Deposit or Withdrawal D/W ? (Press E to Exit):&quot;);
DepORWith = keyboard.nextLine();
} else {
System.out
.println(&quot;---ERRROR ---\nINPUT Y OR N\nWould you Like to withdrawal again? Y/N: &quot;);
Confirmation = keyboard.nextLine();
}
}
WithdrawalTXT.println(&quot;\n--------------------------------------&quot;);
WithdrawalTXT.printf(&quot;\nTotal \t\t\t $%,.2f&quot;, DepTotal);
WithdrawalTXT.close();
}
System.out.printf(&quot;Deposit Total: %,.2d \nWithdrawal Total: %,.2d&quot;, DepTotal, WithTotal);
System.out.print(&quot;\n----------------------------------&quot;);
System.out.print(&quot;\nThank you for Using Old National&quot;);
System.out.print(&quot;\n----------------------------------&quot;);
} else {
System.out.println(&quot;Goodbye!&quot;);
}
} while (!DepORWith.equalsIgnoreCase(&quot;E&quot;));
}
}

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.

Feel free to comment in case of any doubt/issue.

huangapple
  • 本文由 发表于 2020年4月5日 01:34:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61032098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定