2

I'm learning Java for one of my college courses, and I need to know how to pause the output of my program after every 12 iterations of a for loop. I have a loop that iterates 360 times, and I'd like for every 12 items the loop outputs the output to stop until a key is pressed. I have done quite a bit of searching, but everything I've found was in a much more advanced context and I didn't quite understand what else was going on in the code. There is no GUI for this program, and it is so simple that I could just paste all of the code here:

public class MyMain

{      
    static double principal = 200000.00;
    static double interest = .0575;
    static int term = 360;
    static DecimalFormat currency = new DecimalFormat("###,###.##");

    public static void main(String[] args)
    {

        MortgageCalculator mortgageWeek2 = new MortgageCalculator(principal, interest, term);
        double monthlyPayment = mortgageWeek2.calculate();
        System.out.println("Welcome to my Mortgage Calculator.");
        System.out.println("The principal on this loan is $" + currency.format(principal) + ".");
        System.out.println("The interest on this loan is " + (interest * 100) + "%.");
        System.out.println("The term on this loan is " + term + " months (" + term / 12 + " years).");
        System.out.println("Payments on this loan will be $" + currency.format(monthlyPayment));

        double balanceRemaining = 200000.0;



        for (int i = 0; i < 30 * 12; i++)
        {
            double interestPaid = balanceRemaining * .0575 / 12;
            double principalPaid = monthlyPayment - interestPaid;
            balanceRemaining = balanceRemaining - principalPaid;
            System.out.println("Month " + (i + 1) + " \tPayment Amount: $" + currency.format(monthlyPayment) + 
                    "\tInterest Paid: $" + currency.format(interestPaid) + 
                    " \tPrincipal Paid: $" + currency.format(principalPaid) + 
                    "    \tBalance Remaining: $" + currency.format(balanceRemaining));
        }
      }
    }
Dave
  • 75
  • 1
  • 1
  • 11
  • 1
    Hmm, interesting question. Detecting keys pressed from the command line? You can try this: http://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it. If you find an alternative method, you might want to look into Thread.sleep(ms) in a while loop. – Vineet Kosaraju Nov 15 '12 at 04:36

4 Answers4

2

Break your for loop in two segments and add a input statement in the outer loop as below:

    Scanner input = new Scanner(System.in);
    String userInput = null;
    for (int i = 0; i < 30; i++)
    {
      for (int j = 0; j < 12; j++)
      {
        double interestPaid = balanceRemaining * .0575 / 12;
        double principalPaid = monthlyPayment - interestPaid;
        balanceRemaining = balanceRemaining - principalPaid;
        System.out.println("Month " + (i*12+j + 1) + 
                 " \tPayment Amount: $" + currency.format(monthlyPayment) + 
                "\tInterest Paid: $" + currency.format(interestPaid) + 
                " \tPrincipal Paid: $" + currency.format(principalPaid) + 
                "    \tBalance Remaining: $" + currency.format(balanceRemaining));
      }
      userInput = input.nextLine();
    }
    input.close();

Once it stops, you need to press any character and a return key or just the return key.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • A loop in a loop is definitely a step I was missing. I'll have to research those input commands, though. We haven't learned anything about input yet. – Dave Nov 15 '12 at 04:48
  • Doesn't this require the user to press enter? – ApproachingDarknessFish Nov 15 '12 at 04:48
  • @ValekHalfHeart: Yeah. I added that as not in the bottom. – Yogendra Singh Nov 15 '12 at 04:58
  • 2
    I'm fine with the using having to press enter. I'll just have a println that says to press enter to continue. That will be my key. Simple. I just want to research and understand the new commands I've seen here. I won't put anything in my code unless I understand how and why it's used. I actually want to learn Java, not just get a passing grade. =) – Dave Nov 15 '12 at 04:59
  • @Dave: Good spirit!! Just to help you understand, `System.in` is the input stream which reads the inputs from console. If you want, you may use direction methods of System.in e.g. `System.in.read()`. Scanner is a wrapper of an input stream including `System.in` and provides some easy to use methods e.g. readLine();. – Yogendra Singh Nov 15 '12 at 05:03
  • Just wanted to follow up with you to let you know my assignment works great, and has some functionality beyond what was required thanks to your and evan's input here. Also, I understand why it all works, so I've added a new building block to my Java repitoire. Thanks a ton. I'll be using this site more in the future, I'm sure of it. – Dave Nov 15 '12 at 07:30
  • @Dave: Thanks Dave for sharing your feedback. I see many users, who don't even care to share the basic feedback i.e. it worked or not and also don't care about accepting the answer. Keep up the nice spirit!! – Yogendra Singh Nov 15 '12 at 07:33
1

The best way to do this is to make an inner loop that does the printing (has 12 iterations) and wrap that with a loop that waits for input. So you have

Scanner input = new Scanner(System.in);
String in = null;  


for (int j = 0; j < 30; j++) {
      for (int i = 0; i < 12; i++)
      {
        double interestPaid = balanceRemaining * .0575 / 12;
        double principalPaid = monthlyPayment - interestPaid;
        balanceRemaining = balanceRemaining - principalPaid;
        System.out.println("Month " + (i + 1) + " \tPayment Amount: $" + currency.format(monthlyPayment) + 
                "\tInterest Paid: $" + currency.format(interestPaid) + 
                " \tPrincipal Paid: $" + currency.format(principalPaid) + 
                "    \tBalance Remaining: $" + currency.format(balanceRemaining));
    }

    System.out.println("Press any key.");
    in = input.next();

 }
 input.Close();
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • This is a great bit of information for me. I hadn't thought to do this. Now I just need to look over a few things to figure out how to capture input. Simply pressing enter would be enough for me. – Dave Nov 15 '12 at 04:46
  • I just wanted you to know I'd give you both credit for the answer if I could. I gave it to Yog for being a bit more thorough. When I have enough rep I'll surely pass you an upvote, though. Thanks for the help. =) – Dave Nov 15 '12 at 04:53
  • @Dave np. His answer was more thorough and thus more deserving. – evanmcdonnal Nov 15 '12 at 04:56
0

You can user "KeyPress" event to check weather a key is press or not.You can also find out which key is pressed Then you "break" to stop loop iteration...Hope this help

Here is an example

public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    switch( keyCode ) { 
        case KeyEvent.VK_UP:
            // handle up 
            break;
        case KeyEvent.VK_DOWN:
            // handle down 
            break;
        case KeyEvent.VK_LEFT:
            // handle left
            break;
        case KeyEvent.VK_RIGHT :
            // handle right
            break;
     }
} 

you can get keycode of any key from internet easily.

Kashif Hanif
  • 1,718
  • 2
  • 17
  • 29
-1

You just need to use System.in.read(); to read user input. Cheers!

Gonz
  • 1,198
  • 12
  • 26
  • This does nothing to address the waiting for input after every 12 lines. Really he needs to follow my solution or Yogendra's which is basically the same but more complete (I don't know my java cmd line io libraries :o ). – evanmcdonnal Nov 15 '12 at 04:45
  • it's just how to read user input, he migth have to know where to use it. btw, when i answer there where no answers, your's is the best. – Gonz Nov 15 '12 at 04:55