-1

Im trying to keep the FinalBal updated within the while loop but it doesn't. I think I need to add code to keep the FinalBal updated. I'm just praticing for a test. My class in college is basic java. Here is my code:

import java.util.Scanner;

public class TestPraticeBank {


    public static void main(String[] args) {

        int balance = 100;
        int TotalBal;
        int CurBal;
        int amount;

        Scanner keyboard = new Scanner(System.in);
        System.out.print("1. Deposit\n" +
                         "2. Withdrawal\n" +
                         "3. View\n" +
                         "4. Exit\n" +
                         "Enter choice:\n");
        String input = keyboard.nextLine();
        int choice = Integer.parseInt(input);

        while(choice <= 4|| choice >= 5)

        {
            if (choice == 4)
          {
             //System.out.print("Final Balance:"+TotalBal);
             System.exit(choice);
          }
             else if (choice == 3)
            {   
             // System.out.print("Final Balance:"+TotalBal);
            }
             else if (choice == 1)
            {
              System.out.print("Enter the deposit amount:$");
              String deposit = keyboard.nextLine();
              amount = Integer.parseInt(deposit);
              TotalBal = balance + amount;
              System.out.print("Final Balance:$" + TotalBal);
            }
             else if (choice == 2)
            {
              System.out.print("Enter the withdrawal amount:$");
              String withdrawal = keyboard.nextLine();
              amount = Integer.parseInt(withdrawal);
              if (amount <= balance)
                {
             TotalBal = balance - amount;
             System.out.print("Final Balance:$" + TotalBal);
                }
                else
                        {
                           System.out.print("Your withdrawal amount can't be bigger than your current balance");
                        }
            }
            else 
            {
                 System.out.print("Pick only options 1 - 4.");
            }
            System.out.print
                         ("\n1. Deposit\n" +
                         "2. Withdrawal\n" +
                         "3. View\n" +
                         "4. Exit\n" +
                         "Enter choice:\n");
            input = keyboard.nextLine();
            choice = Integer.parseInt(input);
        }  
    }   
}
  • Much more things to correct within your code, but your main issue and the one that asks the question is CurBal += balance when it should be CurBal += TotalBal, because you are crushing the curBal value with balance time after time... – KBorja Oct 25 '14 at 16:36
  • I clean my code a little. I use less variables but the same problem persists. If you can relook at my code give me some hints? – Alex Villalovs Oct 25 '14 at 19:11
  • The same again, every time you are assigning TotalBal = balance + amount, which is 100 + what you enter, drop TotalBal and just use balance += amount or balance -= amount. And take a look to the answer and re-style your code. – KBorja Oct 26 '14 at 09:37
  • I did that and now it works. Thank you. I used to do that in c++ and it would work or maybe I remembered wrong. And reading through other answers here, switch statements are better for these types are programs. – Alex Villalovs Oct 26 '14 at 18:55

3 Answers3

2

Not a direct answer, but a matter of style and readability. The switch() statement is a sweeter way of testing the choices in a case like this, especially as some of the choices nest further if statements. And do...while() means you don't have a duplicate input statement to maintain:

int choice;
do {
    choice = Integer.parseInt(input);
    switch(choice) {
        case 1:
            // statements
            break;
        case 2:
            // statements
            break;
        case 3:
            // statements
            break;
        case 4:
            // do nothing
            break;
        default:
            System.out.print("Pick only options 1 - 4.");
            break;
    }
while (choice != 4);
}

As another point of technique, System.exit() is typically used when your normal program flow can't be followed due to unexpected problems. It's usual to construct the logic so that when a user chooses to exit it is graceful, exiting main() rather than saying "oh I give up". You might have memory to release, files to close, threads to stop, servers to log out of, etc. See previous question When should we call System.exit in Java

Community
  • 1
  • 1
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

I'm not going to give you the answer since this is homework, but here are some things to think about:

          CurBal = amount;

This sets CurBal to the amount they typed in.

          CurBal += balance;

What value is balance supposed to hold? Where is it set? Is it supposed to be updated somewhere? Where is it updated?

          TotalBal = CurBal;

At this point, CurBal will be the amount they typed in, plus whatever is in balance. Will this be correct?

          TotalBal =(TotalBal);

What do you think this statement will do, and why is it here? It actually does nothing. Basically it says "take the value in TotalBal and put it into TotalBal".

ajb
  • 31,309
  • 3
  • 58
  • 84
  • Oops that last statement was accidental. I was trying something out but I didn't delete it completely lol (TotalBal = (TotalBal);. Balance is fixed set to 100. Its under public static void main(String[] args) {, at the top. This is actually my first test and I'm praticing for my second test which is on TUesday. – Alex Villalovs Oct 25 '14 at 17:19
  • Well I updated my code updated my code slightly with less variables. TotalBal should be balance(100) + amount(number typed). I still can't figure out what code am I missing. If I remember correctly, this worked in c++ unless I am forgetting something. Don't tell the answer just yet but do you think I should do next? – Alex Villalovs Oct 25 '14 at 18:34
  • What should you do next? Rewrite the code, bearing in mind what has been written here. It's a bitter pill to swallow, but it's often the best - and most effective - thing to do. – Weather Vane Oct 25 '14 at 19:08
  • So the way this code is written, will not work? I know I can use switch statements but it would have been good to know how write programs in this style. – Alex Villalovs Oct 25 '14 at 19:18
  • You already told us the code will not work. It could work, but it's not in very good style. It's not a large piece of work, and you will learn more from starting over in a new style, re-reading what's been said here, and thinking about each line of code, than you will learn by tinkering with it. And learning by practice is what it's all about, n'est-ce pas? It's not a bank's code, obviously, it's your student exercise, so although you might want a quick result, you'll learn more in the long run. – Weather Vane Oct 25 '14 at 19:41
  • @AlexVillalovs You say "TotalBal should be balance(100) + amount(number typed)." That's what it should be after they enter the _first_ number. What should it be after they enter the second number? Still 100 + number typed, or something else? – ajb Oct 25 '14 at 20:04
  • @ajb So for the first input, 100 + the number is typed. For the second input if choice 1 is still chosen, input1 + the number that is typed again. Kind of like when you go to the bank and deposit money monthly. – Alex Villalovs Oct 25 '14 at 20:40
  • @AlexVillalovs Right, and that's why you can't say `TotalBal` should be `balance + amount`, unless the `balance` will change. If `balance` stays at 100 through the whole program, `TotalBal` will be right only for the first input. – ajb Oct 25 '14 at 23:01
0

There are errors in your program.First of all,it's better to use nextInt() method rather than nextLine() and then parseInt(String).It's going to save 2 lines of code.And about your problem.... Now,in case of a deposit,the deposit amount is added to the initial balance.So,it is going to be balance+=CurBal.The same applies to withdrawal.This total balance is not "CurBal" here, it is the balance after deposit or withdrawal.And one more thing,you wish to perform deposit or withdrawal operations.I do not think there is any need of the variable "CurBal" here.So,it is going to be like this:-

int deposit=keyboard.nextInt();
balance+=deposit;
TotalBal=balance;

And for the withdrawal:-

int withdrawal=keyboard.nextInt();
balance-=withdrawal;
TotalBal=balance;

I think it's better to use this.So,give it a try.

  • I tried that but it get errors when I run the program and press choices 1 or 2. This are the errors:Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:504) at java.lang.Integer.parseInt(Integer.java:527) at TestPraticeBank.main(TestPraticeBank.java:83) Java Result: 1 – Alex Villalovs Oct 25 '14 at 18:39
  • If you're going to do things that way, you have to use `nextInt()` all the time, or be very careful. Mixing `nextInt()` and `nextLine()` can cause problems. After you use `nextInt()`, if the user types just one integer on the line, the next `nextLine()` will return `""`, so you will need an extra `nextLine()` to clear out the `""`. My suggestion is to go back to what you had before--it was working. – ajb Oct 25 '14 at 20:09
  • I'm sorry but I do not understand you. – Alex Villalovs Oct 25 '14 at 20:34
  • @AlexVillalovs In that case, just go back to using `nextLine()` since it worked. (StackOverflow gets about 50 questions a day from people who try to use both `nextInt()` and `nextLine()` in the same program and run into problems. It can be done, but if you don't understand how, better to to stick to just one or the other for now.) – ajb Oct 25 '14 at 22:59
  • @AlexVillalovs In my answer,I meant to say that you should not use nextLine() at all.The examples I had given were just for computing deposit and withdrawal.It's better to use nextInt() in every case.So,try it out. – Saptak Bhattacharya Oct 26 '14 at 07:58