Results 1 to 20 of 20
Thread: infinite loop
- 03-02-2011, 10:00 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
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 methodLast edited by Eranga; 03-02-2011 at 10:10 AM. Reason: code tags added
- 03-02-2011, 10:12 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 03-02-2011, 10:22 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
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
- 03-02-2011, 10:27 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 03-02-2011, 10:27 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 03-02-2011, 10:31 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
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
- 03-02-2011, 10:32 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
*and (not end) :)
- 03-02-2011, 10:57 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I think this cause an error.
because inside the block I cannot see that you've manipulate the value of it.Java Code:while (accountTypeChoice != 4);
- 03-02-2011, 10:58 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
By the how did you debug the code? By putting some breakpoints with your IDE?
- 03-02-2011, 11:03 AM #10
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
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
{
- 03-02-2011, 11:16 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 03-02-2011, 11:25 AM #12
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
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!
- 03-02-2011, 11:31 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Hmm, okay let me check the code segment again.
- 03-02-2011, 11:33 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Enter the input as 5 and check.
- 03-02-2011, 11:41 AM #15
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
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
- 03-02-2011, 11:54 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Could you send me the complete code you've right now? It's easy for me.
- 03-02-2011, 12:01 PM #17
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
take a look at your private inbox, i've sent you a message there with regards to e-mail address...
- 03-02-2011, 05:06 PM #18
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
You have to control the choice in each switch.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
- 03-03-2011, 05:10 PM #19
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
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.. :(
- 03-06-2011, 02:28 AM #20
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, once disable the upper loop (execute only once) it works fine.
Similar Threads
-
how to end infinite loop
By search4survival in forum New To JavaReplies: 14Last Post: 10-25-2010, 08:59 AM -
Infinite loop
By jDennis79 in forum New To JavaReplies: 7Last Post: 08-13-2010, 11:45 PM -
Infinite Loop
By bosoxfan in forum New To JavaReplies: 3Last Post: 02-22-2010, 01:34 AM -
Infinite Loop
By rclausing in forum New To JavaReplies: 2Last Post: 01-23-2010, 10:11 PM -
java recursion infinite loop
By tony404 in forum Advanced JavaReplies: 9Last Post: 10-03-2008, 01:16 PM


LinkBack URL
About LinkBacks

Bookmarks