Results 1 to 20 of 20

Thread: infinite loop

  1. #1
    javapink is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Exclamation infinite loop

    I am working on the method below and io'm getting an ifinite loop.
    very urgent please, because i have a deadline for school project!
    thanks so so much for those who will help!

    a bit long but i'm starving for help.

    just a background: it is all about a banking system.
    the method below deals with the creation of a new account.
    There is the use of linked lists as parameters,
    the user should choose to create either a Current, Savings or Fixed Account.
    However as you will notice, there are 3 types of fixed accounts. I tried to allow the choice of fixed accounts by the means of a 'switch' block, however i thought that it was the problem to cause the infinite loop and removed it.

    CODE BELOW:
    Java Code:
    public static void createAccount(List<Current> currentListIn, List<Savings> savingsListIn, List<Fixed> fixedListIn) 
    
        {
            System.out.println("                                               ACCOUNT CREATOR");
            System.out.println("                                                     MENU");
            System.out.println();
            System.out.println("                                   1. Create a new Current Account");
            System.out.println("                                   2. Create a new Savings Account");
            System.out.println("                                   3. Create a new Fixed-Term Account");
            System.out.println("                                   4. Exit");
            System.out.println();
            System.out.print("                                    ENTER A CHOICE FROM 1-4: ");
            
            Scanner sc = new Scanner (System.in);
            Scanner scString = new Scanner (System.in);
            Scanner scLine = new Scanner (System.in);
            int accountTypeChoice = sc.nextInt();
            
           do
            {
              
            switch (accountTypeChoice)
            {
         case 1:
             		
    
    		System.out.print("Enter Account Number:");
    		String accountNumberIn = scString.next();
    		System.out.println();
    		
    		System.out.print("Enter Account holder's Name:");
    		String holdersNameIn = scLine.nextLine();
    		System.out.println();
    		
    		System.out.print("Enter Account holder's Surname:");
    		String holdersSurnameIn = scLine.nextLine();
    		System.out.println();
    		
    		System.out.print("Enter Account holder's ID Card Number:");
    		String holdersIDIn = scString.next();
    		if (holdersIDIn.length() > 10)                                            //limits the ID number to be not longer than 10 characters
    		{
    			System.out.print("Ten characters maximum - please re-enter: ");
    			holdersIDIn = scString.next();
    		}
    		for(int i = holdersIDIn.length() + 1; i <= 10; i++)
    		{
    			holdersIDIn = holdersIDIn.concat(" ");
    		}
    		System.out.println();
    		
    		System.out.print("Enter Account holder's address:");
    		String addressIn = scLine.nextLine();
    		System.out.println();
    		
    		System.out.print("Enter Account holder's land-line number:");
    		String telIn = scString.next(); 
    		if (telIn.length() > 15)
    		{
    			System.out.print("Ten characters maximum - please re-enter: ");
    			telIn = scString.next();
    		}
    		for(int i = telIn.length() + 1; i <= 15; i++)
    		{
    			telIn = telIn.concat(" ");
    		}
    		System.out.println();
    		
    		System.out.print("Enter Account holder's mobile number:");
    		String mobIn = scString.next();
    		if (mobIn.length() > 10)
    		{
    			System.out.print("Ten characters maximum - please re-enter: ");
    			mobIn = scString.next();
    		}
    		for(int i = mobIn.length() + 1; i <= 15; i++)
    		{
    			mobIn  = mobIn.concat(" ");
    		}
    		System.out.println();
    		
    		System.out.print("Enter Account holder's E-mail address:");
    		String emailIn = scString.next();
    		System.out.println();
    
    		System.out.print("Enter Account's balance:");
    		double balanceIn = sc.nextDouble();
    		System.out.println();
    		
    
                    
                    Current tempCurrent = new Current(accountNumberIn, holdersNameIn, holdersSurnameIn, holdersIDIn, addressIn, telIn, mobIn, emailIn, balanceIn);
                    
                    currentListIn.add(tempCurrent);
                    System.out.println("ACCOUNT WAS SUCCESSFULLY CREATED");
             break;
                
                
      
         case 2:
              
                    
            System.out.print("Enter Account Number:");
            accountNumberIn = scString.next();
            System.out.println();
            
            System.out.print("Enter Account holder's Name:");
            holdersNameIn = scLine.nextLine();
            System.out.println();
            
            System.out.print("Enter Account holder's Surname:");
            holdersSurnameIn = scLine.nextLine();
            System.out.println();
            
            System.out.print("Enter Account holder's ID Card Number:");
            holdersIDIn = scString.next();
            if (holdersIDIn.length() > 10)
            {
                System.out.print("Ten characters maximum - please re-enter: ");
                holdersIDIn = scString.next();
            }
            for(int i = holdersIDIn.length() + 1; i <= 10; i++)
            {
                holdersIDIn = holdersIDIn.concat(" ");
            }
            System.out.println();
            
            System.out.print("Enter Account holder's address:");
            addressIn = scLine.nextLine();
            System.out.println();
            
            System.out.print("Enter Account holder's land-line number:");
            telIn = scString.next(); 
            if (telIn.length() > 15)
            {
                System.out.print("Ten characters maximum - please re-enter: ");
                telIn = scString.next();
            }
            for(int i = telIn.length() + 1; i <= 15; i++)
            {
                telIn = telIn.concat(" ");
            }
            System.out.println();
            
            System.out.print("Enter Account holder's mobile number:");
            mobIn = scString.next();
            if (mobIn.length() > 10)
            {
                System.out.print("Ten characters maximum - please re-enter: ");
                mobIn = scString.next();
            }
            for(int i = mobIn.length() + 1; i <= 15; i++)
            {
                mobIn  = mobIn.concat(" ");
            }
            System.out.println();
            
            System.out.print("Enter Account holder's E-mail address:");
            emailIn = scString.next();
            System.out.println();
    
            System.out.print("Enter Account's balance:");
            balanceIn = sc.nextDouble();
            System.out.println();
    
                    SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy"); //Adding 3 days to current date
                    Calendar cal = Calendar.getInstance();
                    cal.add(Calendar.DATE, 365);    
                    String maturityDateIn = dateformat.format(cal.getTime());
                     
                    System.out.print("Enter Account's interest rate:");
                    double rateIn = sc.nextDouble();
                    Savings tempSavings = new Savings(accountNumberIn, holdersNameIn, holdersSurnameIn, holdersIDIn, addressIn, telIn, mobIn, emailIn, balanceIn, rateIn, maturityDateIn);
                    System.out.println();
                    
                    savingsListIn.add(tempSavings);
                    System.out.println("ACCOUNT WAS SUCCESSFULLY CREATED");
                    break;
                
                    
                case 3:
               {    
            System.out.print("Enter Account Number:");
            accountNumberIn = scLine.next();
            System.out.println();
            
            System.out.print("Enter Account holder's Name:");
            holdersNameIn = scLine.nextLine();
            System.out.println();
            
            System.out.print("Enter Account holder's Surname:");
            holdersSurnameIn = scLine.nextLine();
            System.out.println();
            
            System.out.print("Enter Account holder's ID Card Number:");
            holdersIDIn = scString.next();
            if (holdersIDIn.length() > 10)
            {
                System.out.print("Ten characters maximum - please re-enter: ");
                holdersIDIn = scString.next();
            }
            for(int i = holdersIDIn.length() + 1; i <= 10; i++)
            {
                holdersIDIn = holdersIDIn.concat(" ");
            }
            System.out.println();
            
            System.out.print("Enter Account holder's address:");
            addressIn = scLine.nextLine();
            System.out.println();
            
            System.out.print("Enter Account holder's land-line number:");
            telIn = scString.next(); 
            if (telIn.length() > 15)
            {
                System.out.print("Ten characters maximum - please re-enter: ");
                telIn = scString.next();
            }
            for(int i = telIn.length() + 1; i <= 15; i++)
            {
                telIn = telIn.concat(" ");
            }
            System.out.println();
            
            System.out.print("Enter Account holder's mobile number:");
            mobIn = scString.next();
            if (mobIn.length() > 10)
            {
                System.out.print("Ten characters maximum - please re-enter: ");
                mobIn = scString.next();
            }
            for(int i = mobIn.length() + 1; i <= 15; i++)
            {
                mobIn  = mobIn.concat(" ");
            }
            System.out.println();
            
            System.out.print("Enter Account holder's E-mail address:");
            emailIn = scString.next();
            System.out.println();
    
            System.out.print("Enter Account's balance:");
            balanceIn = sc.nextDouble();
            System.out.println();
            
    
                    System.out.println("                                   CHOOSE THE TYPE OF FIXED-TERM ACCOUNT");
                    System.out.println();
                    System.out.println("                                   1. 3 Month Fixed-Term Account");
                    System.out.println("                                   2. 1 Year Fixed-Term Account");
                    System.out.println("                                   3. 3 Year Fixed-Term Account");
                    System.out.println("                                   4. Quit");
                    System.out.println();
                    System.out.print("                                     ENTER A CHOICE FROM 1-4: ");
                    
                    int choice = sc.nextInt();
                    
                
                    if (choice == 1)
                    {
                        rateIn = 1.2;
                        
                        SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy"); //producing the current date
                        Date now = new Date();
                        String startDateIn = sdfDate.format(now); //the opening date of the 3 Months Fixed-Term Account 
                         
                        dateformat = new SimpleDateFormat("dd/MM/yyyy"); //Adding 92 days, approximately equal to 3 months to current date
                        cal = Calendar.getInstance();
                        cal.add(Calendar.DATE, 92); 
                        String endDateIn = dateformat.format(cal.getTime()); //the approximate closing date of the 3 Months Fixed-Term Account
                        
                        System.out.println("NOTE: This account has opened today, " +startDateIn);
                        System.out.println("      This account expires on: " +endDateIn);
                        
                        Fixed tempFixed = new Fixed(accountNumberIn, holdersNameIn, holdersSurnameIn, holdersIDIn, addressIn, telIn, mobIn, emailIn, balanceIn, rateIn, choice, startDateIn, endDateIn);
                        
                        fixedListIn.add(tempFixed);
                        System.out.println("ACCOUNT WAS SUCCESSFULLY CREATED");
                        break;
                    }
                    
                    else if (choice == 2)
                    {
                        rateIn = 2.0;
                        
                        SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy"); //producing the current date
                        Date now = new Date();
                        String startDateIn = sdfDate.format(now); //the opening date of the 3 Months Fixed-Term Account
                    
                        dateformat = new SimpleDateFormat("dd/MM/yyyy"); //Adding 365 days, equal to 1 year (approximately 1 year if a leap year) to current date
                        cal = Calendar.getInstance();
                        cal.add(Calendar.DATE, 365);    
                        String endDateIn = dateformat.format(cal.getTime()); //the approximate closing date of the 1 Year Fixed-Term Account
                        
                        System.out.println("NOTE: This account has opened today, " +startDateIn);
                        System.out.println("      This account expires on: " +endDateIn);
                        
                        Fixed tempFixed = new Fixed(accountNumberIn, holdersNameIn, holdersSurnameIn, holdersIDIn, addressIn, telIn, mobIn, emailIn, balanceIn, rateIn, choice, startDateIn, endDateIn);
                        
                        fixedListIn.add(tempFixed);
                        System.out.println("ACCOUNT WAS SUCCESSFULLY CREATED");
                        break;
                    }
                    
                    else if (choice == 3)
                    {
                        rateIn = 2.5;
                        
                        SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy"); //producing the current date
                        Date now = new Date();
                        String startDateIn = sdfDate.format(now); //the opening date of the 3 Months Fixed-Term Account
                        
                        dateformat = new SimpleDateFormat("dd/MM/yyyy"); //Adding 3 years (approximately 3 years if a leap year is not involved) to current date
                        cal = Calendar.getInstance();
                        cal.add(Calendar.DATE, 1095);
                        String endDateIn = dateformat.format(cal.getTime()); //the approximate closing date of the 3 Years Fixed-Term Account
                        
                        System.out.println("NOTE: This account has opened today, " +startDateIn);
                        System.out.println("      This account expires on: " +endDateIn);
                        
                        Fixed tempFixed = new Fixed(accountNumberIn, holdersNameIn, holdersSurnameIn, holdersIDIn, addressIn, telIn, mobIn, emailIn, balanceIn, rateIn, choice, startDateIn, endDateIn);
                        
                        fixedListIn.add(tempFixed);
                        System.out.println("ACCOUNT WAS SUCCESSFULLY CREATED");
                        break;
                    }
                    else if (choice == 4)
                    {
                        break;
                    }    
                    else 
                    {
                        System.out.println("No such choice");   
                        break;
                    }    
                    
               } //ends 'case 3' of first switch method  
                    
                case 4:
                        
                        break;
                        
                   default: 
                    
                        System.out.println("No such choice");  
                        break;
                    
            } //ends first switch of method
            } while (accountTypeChoice != 4);
        }   //ends method
    Last edited by Eranga; 03-02-2011 at 11:10 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all, please use code tags next time when you are posting code segment on the forum. Unformatted codes are really hard to read. If you don't know how to do that, please check my forum signature. You can find the relevant link.

    Secondly, your code is too lengthy. So it's not an easy task to find where the error was. So can you post the code segment or the loop as you said that leads to infinite execution.

  3. #3
    javapink is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    kinda compleceted and i'm really pressed with time.
    i have to get my prgram woring and prepare it documentation (flowcharts, user's manual, pseudo code, testing etc..) deadline is friday morning :(

    can i e-mail the program to you or anything which is less demanding please?

    thanks much, i appreciate your efficiant reply

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    The thing is this, I'm not here in the forum to write code for others. But I like to help others to solve there problem in a way that I can help. Because I'm not a genius in Java. At the same time people who post there questions should be more clever.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  6. #6
    javapink is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    oh ok then .. there's nothing i can do :(

    i've checked it a million times, and i am finding nothing wrong why an infinite loop could be caused.

    I sort of assured myself that it is an infinite loop because when i run the program end enter the option which strikes the default case for the 'switch' block, it goes on forever if the program is not manually terminated.

    still thanks a lot anyway, for putting interest into my query

  7. #7
    javapink is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    *and (not end) :)

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I think this cause an error.

    Java Code:
    while (accountTypeChoice != 4);
    because inside the block I cannot see that you've manipulate the value of it.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  10. #10
    javapink is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    i just checked it with the compiler.. i was never thought how to insert breakpoints .. :/

    look,

    i've manipulated it, at this point here in red, at the very beginning, am i right? it is when i asked the user to enter a choice.

    System.out.println(" 4. Exit");
    System.out.println();
    System.out.print(" ENTER A CHOICE FROM 1-4: ");

    Scanner sc = new Scanner (System.in);
    Scanner scString = new Scanner (System.in);
    Scanner scLine = new Scanner (System.in);
    int accountTypeChoice = sc.nextInt();

    do
    {

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yeah, in that line you assign a value to the accountTypeChoice . Then enter to the do-while loop. And in while loop you check the condition that accountTypeChoice != 4. Say the input is not 4, then you are in a endless loop.

    Important thing to notice, do-while loop execute once before check the condition.

  12. #12
    javapink is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    i have added the default case to satisfy that criteria...
    take a look below, highlighted in red:


    default:

    System.out.println("No such choice");
    break;


    it is at the very end of the code

    i've been looking at the same chunk of lines from around 11.30pm of yesterday, no sleep up till now... and no idea were this infinite loop is coming from.

    may God bless you, the help you are giving me cannot be given any value!

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  15. #15
    javapink is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    not a problem at all, take your time

    perhaps i cannot repay you for this good, but God will...
    if you manage to take me out from this trouble you will be God sent today lol

    thanks much

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  17. #17
    javapink is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    take a look at your private inbox, i've sent you a message there with regards to e-mail address...

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    The error cause that you are executing that do-while loop from another do-while loop in the main method. So that loop is never ends.

    Java Code:
    do { //starts a 'do...while...' loop
                    switch (choice) { //starts a 'switch' block
                        case 1: {
                            createAccount(currentList, savingsList, fixedList);
                            break;
                        }
    
                        case 2: {
                            deleteAccount(currentList, savingsList, fixedList);
                            break;
                        }
    
                        case 3: {
                            retrieveAccount(currentList, savingsList, fixedList);
                            break;
                        }
    
                        case 4: {
                            modifyAccount(currentList, savingsList, fixedList);
                            break;
                        }
    
                        case 5: {
                            transactions(currentList, savingsList, fixedList);
                            break;
                        }
    
                        case 6: {
                            System.out.println("Changes done will be saved on the file. Press any key to continue..");
                            char anyKey = sc.next().charAt(0);
    
                            writeCurrentList(currentList);
                            writeSavingsList(savingsList);
                            writeFixedList(fixedList);
                            System.out.println("File saved");
                            break;
                        }
    
                    } //ends a switch block
                } while (choice != 6); //ends a 'do...while...' loop
    You have to control the choice in each switch.

  19. #19
    javapink is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default won't work

    this is devastating!

    did you try to edit the code and work yourself?

    cos i tried to removed the second do while loop and its all in vain...still with the infinite loop.
    today is my last day...i still have to document flowcharts..pseudo code and testing by tomorrow morning.. :(

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. how to end infinite loop
    By search4survival in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 09:59 AM
  2. Infinite loop
    By jDennis79 in forum New To Java
    Replies: 7
    Last Post: 08-14-2010, 12:45 AM
  3. Infinite Loop
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 02:34 AM
  4. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 11:11 PM
  5. java recursion infinite loop
    By tony404 in forum Advanced Java
    Replies: 9
    Last Post: 10-03-2008, 02:16 PM

Tags for this Thread

Posting Permissions

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