Results 1 to 4 of 4

Thread: Infinite Loop

  1. #1
    bosoxfan is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default Infinite Loop

    Hello everyone,

    I have to write this program for a class. The point of the program is to give the user a menu and select an option to deposit, withdraw or check bank account balance. I have it working properly for the most part. The problem I am running into is when I make my selection it is sending the program into an infinite loop. I thought I had my brackets in the correct places but I am beginning to realize I probably do not and hopefully someone here can help me. Below is the code for my program. Thanks in advance for any assistance.

    Java Code:
    import java.util.Scanner; 
    public class bankprogram1 {
        public static void main (String[] args){
            
            Scanner input = new Scanner (System.in); 
            System.out.println("Please select the action you would like to perform"); 
            System.out.println("Please select 1 to deposit funds to your account."); 
            System.out.println("Please select 2 to withdraw funds from your account."); 
            System.out.println("Please select 3 to check your account balance."); 
            System.out.println("Please select 4 to exit."); 
            
            double balance = 1000.00; 
                int choice = input.nextInt(); 
                do
                {
                    if (choice == 1){
                        System.out.println("Please enter the amount of your deposit."); 
                        double deposit = input.nextDouble();  
                        System.out.println("Your new balance is "+ (deposit + balance)); 
                    }
                    
                    else if (choice == 2){
                        System.out.println("Please enter the amount you would like to withdraw."); 
                        double withdraw = input.nextDouble();
                        System.out.println("Your new balance is " + (balance - withdraw));
                    }
                    
                    else if (choice == 3){
                        System.out.println("Your balance is" + balance); 
                    }
                    
                    else if (choice != 4){ 
                        System.out.println ("You have entered an invalid selection."); 
                    }
                }
        while (balance != 0); 
        }
    }
    Moderator edit: code tags added around posted code to aid in its readability.
    Last edited by Fubarable; 02-22-2010 at 02:15 AM. Reason: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Perhaps you want to allow the user to change choice inside the loop. Also your loop exit condition is that balance is not 0 but you never change balance within the loop, so since it will remain 1000 forever and thus never be 0, the loop will never end.

    Also, please look at my signature below regarding use of code tags.

    Much luck!

  3. #3
    bosoxfan is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    Fubarable,

    Thank you for the tip about using the code tags. That was never brought to my attention previously. Also, I think the problem I am having is I am not sure where to put the brackets so that the user makes their choice inside the loop. At first that is what I have thought I was doing but I now see that isnt the case.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bosoxfan View Post
    Fubarable,

    Thank you for the tip about using the code tags. That was never brought to my attention previously. Also, I think the problem I am having is I am not sure where to put the brackets so that the user makes their choice inside the loop. At first that is what I have thought I was doing but I now see that isnt the case.
    Think less about "where to put the brackets" and more about what code to put inside the brackets. Think logically on this and you should be able to figure this out.

Similar Threads

  1. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 11:11 PM
  2. Infinite running loop problems
    By BigDummy in forum New To Java
    Replies: 5
    Last Post: 10-14-2009, 07:39 AM
  3. Infinite loop when reading text file
    By MartinMorrison in forum New To Java
    Replies: 2
    Last Post: 08-03-2009, 05:36 PM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM
  5. java recursion infinite loop
    By tony404 in forum Advanced Java
    Replies: 9
    Last Post: 10-03-2008, 02:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •