Results 1 to 9 of 9

Thread: pausing loops?

  1. #1
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default pausing loops?

    is there a way to pause a loop besides thread sleep? can a loop be paused by iterations of lines threw the loop? IE can it be paused every like 16 or 20 lines for a certain amount of time?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    can it be paused every like 16 or 20 lines for a certain amount of time
    If you want to leave the loop as it is, then the only way I know is by Thread sleep().
    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.

  3. #3
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    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
    }

  4. #4
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    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!

  5. #5
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    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);}
      }
     }
    }

  6. #6
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    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.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    I just wanted to pause my loop for every year, show the user the payments per year, then continue with the next year
    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.

  8. #8
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    yea, that is a little to advanced for me right now, maybe in a couple weeks i will get it :D

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    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

  1. Pausing a Thread
    By Lord Voldemort in forum New To Java
    Replies: 14
    Last Post: 05-22-2011, 05:45 AM
  2. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  3. For loops and while loops
    By patz in forum New To Java
    Replies: 1
    Last Post: 03-20-2011, 06:32 PM
  4. Pausing game when user minimizes window
    By tecno40 in forum Java Applets
    Replies: 1
    Last Post: 12-29-2010, 06:37 PM
  5. Help with loops
    By pg5678pg in forum New To Java
    Replies: 8
    Last Post: 10-17-2010, 07:51 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
  •