I have one, reoccurring issue that arises each time I run this incomplete program I am working on; I have spent much time attempting to locate the bug. I am convinced it arises from a simple error.

Problem: User has made a "deposit" but when the User makes a "withdrawal", the "balance" is an incorrect and negative number always.

Any help in or push in the right direction would be greatly appreciated. Thank-you in advance.

import java.io.*;

final class readString {

   public static void main (String[] args) {

      //  Intro
      System.out.print("Welcome to World Bank. Please make a selection from the following and selct the Enter key:\n 1)Checking\n 2)Savings\n 3)Loans\n Choice:");

      //  Open Input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      String choice1 = "1";
      String choice2 = "2";
    

      String deposit1 = "1";
      String deposit2;
     


      String withdrawal1 ="1";
      String withdrawal2 = null;

      
      String response1 = "Checking";
      String response2 = "Savings";
      String response3 = "Loans";
   

      try {
         choice1 = br.readLine();
        

      if (choice1.equals("1"))
      {
         System.out.println("You chose " + response1);
         System.out.println("Please choose 1 for deposit or 2 for withdrawal:");
         deposit1 = br.readLine();
         System.out.println("Please make your deposit:");
         deposit2 = br.readLine();
         System.out.println("You deposited " + deposit2 + ".");
         System.out.println("Would you like to make a withdrawal? Here you may make a withdrawal and see your balance. Enter 1 to go to Withdrawal area.");
         deposit2 = br.readLine();
         System.out.println("You chose " + withdrawal1);
         System.out.println("Please enter the amount you would like to withdrawal: ");
         withdrawal2 = br.readLine();
         System.out.println("You have withdrawn: $" + withdrawal2 + ".00.");
         
        
         
         
         
         String convert1 = deposit2;
         int newdeposit1 = Integer.parseInt(convert1);
         
         
         String convert2 = withdrawal2;
         int newWithdrawal = Integer.parseInt(convert2);

         
   
         {
          int Balance1 = newdeposit1 - newWithdrawal;
          System.out.println("Your balance is: $" + Balance1 + ".00.");
         }
        
        
      }



  
      if (choice1.equals("2"))
      {
         System.out.println("You chose " + response2);
      }

       
      if  (choice1.equals("3"))
      {
        System.out.println("You chose " + response3);
      }
         
       }


       catch (IOException ioe) {
       System.out.println("Error deteceted.");
       System.exit(1);
      }


Problem: User has made a "deposit" but when the User makes a "withdrawal", the "balance" is an incorrect and negative number always.

Bug Fixed. Solved.

You are introducing to many variables and confusing your self. Just quick simplification

import java.io.*;

final class ReadString {

   public static void main (String[] args) {

      //  Intro
      System.out.print("Welcome to World Bank. Please make a selection from the following and selct the Enter key:\n 1)Checking\n 2)Savings\n 3)Loans\n Choice:");

      //  Open Input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      String choice1 = "1";
      String choice2 = "2";
    

      String deposit1 = "1";
      int deposit;
     


      String withdrawal1 ="1";
      int withdrawal;

      
      String response1 = "Checking";
      String response2 = "Savings";
      String response3 = "Loans";
   

      try {
         choice1 = br.readLine();
        

	      if (choice1.equals("1"))
	      {
	         System.out.println("You chose " + response1);
	         System.out.println("Please choose 1 for deposit or 2 for withdrawal:");
	         deposit1 = br.readLine();
	         
	         System.out.println("Please make your deposit:");
	         deposit = Integer.parseInt(br.readLine());
	         System.out.println("You deposited " + deposit + " .");
	         System.out.println("Would you like to make a withdrawal? Here you may make a withdrawal and see your balance. Enter 1 to go to Withdrawal area.");
	         //deposit2 = br.readLine();
	         System.out.println("You chose " + withdrawal1);
	         System.out.println("Please enter the amount you would like to withdrawal: ");
	         withdrawal = Integer.parseInt(br.readLine());
	         System.out.println("You have withdrawn: $" + withdrawal + ".00.");
	         
	         
	         /*OVER-COMPLICATING YOUR LIFE
	          *String convert1 = deposit2;
	         int newdeposit1 = Integer.parseInt(convert1);
	         String convert2 = withdrawal2;
	         int newWithdrawal = Integer.parseInt(convert2);*/	     
	  
	         //{ What is purpose for this?
	          deposit = deposit - withdrawal;
	          System.out.println("Your balance is: $" + deposit); //+ ".00.");
	         //} What is purpose for this?   
	      }
	      else if (choice1.equals("2"))
	      {
	         System.out.println("You chose " + response2);
	      }
	      else if  (choice1.equals("3"))
	      {
	        System.out.println("You chose " + response3);
	      }
      }
      catch (IOException ioe) {
      	System.out.println("Error deteceted.");
        System.exit(1);
      }
   }
}

You do not need to introduce every time new variable to read user input and then progress in your computation.

To make it simpler introduction of new class Account for start like

public class Account{
	
	private int balance = 0;
	
	public Account(){}
	
	public void deposit(int deposit){
		balance = balance + deposit;
	}
	
	public void witdraw(int withdraw){
		balance = balance - withdraw;
	}
	
	public int getBalance(){
		return balance;
	}
}

Then break your if statement into sub-methods which represents a block of command expected to be executed when appropriate choice is made...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.