Results 1 to 9 of 9
Thread: pausing loops?
- 07-05-2011, 09:44 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 52
- Rep Power
- 0
- 07-05-2011, 10:17 PM #2
If you want to leave the loop as it is, then the only way I know is by Thread sleep().can it be paused every like 16 or 20 lines for a certain amount of time
Can you explain why you want/need to do this?
You need to redesign you app if you think you have a need for this.
Break you loop up into separate pieces and use a Timer or other scheme to control when the different pieces are executed.
- 07-05-2011, 10:30 PM #3
Are you talking about pausing after 16 iterations of the loop? Something like this might work. Although using Thread.sleep() is a lot easier.
Java Code:public static void main(String args[]) { for(int i = 1;i <= 160;i++) { if(i % 16 == 0) // Check if this iteration is 16 or a multiple of 16 wait(2); // Wait for two seconds else continue; // Continue with the loop } } public static void wait(int seconds) { long waitTime = seconds * 1000000000; // Convert to nano seconds long start = System.nanoTime(); // Starting time while(System.nanoTime() - start < waitTime); // Loop until we have waited long enough }
- 07-05-2011, 10:39 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 52
- Rep Power
- 0
I was only curious if there was a way to do this without using threadsleep. I just wanted to pause my loop for every year, show the user the payments per year, then continue with the next year. This was jsut something i thought would make my program a little more cool, but its just an extra thing i thought about trying to do, but when i tried the thread.sleep it just slowed it down a lot, but didnt seem to pause it every so often... This is the next thing I was going to try and tackle... BUT, on a positive note I FINALLY got the rest of the program to work accordingly!! Im in the process of making it more readable, so once i get my indents right i will post it for ya!
- 07-05-2011, 10:48 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 52
- Rep Power
- 0
ok here is my java child....
It ask to if the user wants to enter loan depending on yes/no, then ask for amounts, calculates payments, then shows amortization of the loan, then ask user if they want to enter another
loan or exit!!
Is the indentation right?
Java Code:import javax.swing.*; public class MortgageApp3 { public static void main(String args[]) { double loan, interest_yr, years, interest_mo, loan_pmts, payments, new_balance, interest, interestamt, principal, principalamt, payamt, balance,cont; int p; //Above is all the varaibles used for calculations String loanamt, interest_peryr, yearstot, pay_amt, restart; //The above declares the strings for the program cont = JOptionPane.showConfirmDialog(null, "Would you like to enter a loan?", "Loan Calcuator", JOptionPane.YES_NO_OPTION); if(cont==JOptionPane.NO_OPTION){System.exit(0);} while(cont==JOptionPane.YES_OPTION){ loanamt= JOptionPane.showInputDialog("Enter Loan Amount"); //Obtains input from user for loan amount interest_peryr=JOptionPane.showInputDialog("Enter the Interest Percentage: Example 5.6"); //Obtains input from user for interest % yearstot=JOptionPane.showInputDialog("Enter Loan Period in Years"); //Obtains input for length of loan // Below is the code to parse stings into doubles loan=Double.parseDouble(loanamt); interest_yr=Double.parseDouble(interest_peryr); years=Double.parseDouble(yearstot); interest_mo = (interest_yr/12.0)/100.0; // Turns percent whole numbers into decimals loan_pmts= years * 12; //Gets total amount of months the loan will last after user inputs length of loan in years payments = (loan*interest_mo/(1- Math.pow((1+interest_mo),-loan_pmts))); //Loan calculation JOptionPane.showMessageDialog (null, String.format( "Your payment is: $ %.2f ", + payments)); //Shows payment with 2 decimal places principalamt= payments - (loan *interest_mo); //calculates principle JOptionPane.showMessageDialog(null, String.format( "your prinicple paid is: $ %.2f ", + principalamt)); //shows principle with 2 decimal places interestamt= payments-principalamt ; //calculates interest JOptionPane.showMessageDialog(null, String.format( "your interest paid is: $ %.2f ", + interestamt)); //shows interest with 2 decimal places //below is the loop p=1; balance=loan; for(int month=0;month<(years*12);month++) { interest=balance*interest_mo; //System.out.println("interest "+interest); principal=payments-interest; //System.out.println("principal "+ principal); System.out.println("For Payment: " +p++ ); System.out.println( String.format("Principal paid is: $ %.2f", + principal)); balance=balance-principal; System.out.println( String.format("Remaining balance is: $ %.2f",+ balance)); } cont = JOptionPane.showConfirmDialog(null, "Want to enter another loan?", "Loan Calcuator", JOptionPane.YES_NO_OPTION); if(cont==JOptionPane.NO_OPTION) System.out.println("Good Bye!"); {System.exit(0);} } } }
- 07-05-2011, 10:55 PM #6
No that is not correct indenting. While indenting isn't technically required, the JRE doesn't care, it greatly helps the readability of your code. Also you may want to take a look here for variable naming conventions in Java. Again they aren't required but it helps readability.
- 07-05-2011, 11:18 PM #7
That would take a redesign. Make a method to generate the output for a specified year. Have a GUI with a button that calls the method to display the data for the next year and return. The user then looks at the data. When wanting to move on, he presses the button, the method is called and the next years data is displayed. The waiting is taken care of by the user. Nothing happens (the code waits) until the user presses the button.I just wanted to pause my loop for every year, show the user the payments per year, then continue with the next year
- 07-05-2011, 11:24 PM #8
Member
- Join Date
- Jun 2011
- Posts
- 52
- Rep Power
- 0
yea, that is a little to advanced for me right now, maybe in a couple weeks i will get it :D
- 07-05-2011, 11:29 PM #9
If you are using console for the user interface, you could show the data, the print out a message asking the user to press Enter when he wants to see the next years data. You program would try to read from the console and would block/wait until the user pressed Enter.
Write a small test program using the Scanner class and its methods to see how to do this.
Similar Threads
-
Pausing a Thread
By Lord Voldemort in forum New To JavaReplies: 14Last Post: 05-22-2011, 04:45 AM -
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 04:02 AM -
For loops and while loops
By patz in forum New To JavaReplies: 1Last Post: 03-20-2011, 05:32 PM -
Pausing game when user minimizes window
By tecno40 in forum Java AppletsReplies: 1Last Post: 12-29-2010, 05:37 PM -
Help with loops
By pg5678pg in forum New To JavaReplies: 8Last Post: 10-17-2010, 06:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks