2 years ago

#62758

test-img

user17067764

Not printing lines until after completion Java

I created a mortgage calculator which works just fine but I don't want to use println() when asking for user input. I want the user to be able to type on the same line; however, everytime i use print() the user input is taken but the print statements do not get read into the output until the very end.

Example of Current Output

2000
4.12
10
Principal: Annual Interest Rate: Period (years): Mortgage: $20.36

Current Code

package demos1;

import java.text.NumberFormat;
import java.util.Scanner;

/**
 *
 * @author nicka
 */
public class MortgageCalculator {

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        final byte MONTHS = 12;
        final byte PERCENTAGE = 100;
        
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Principal: ");
        double principal = scanner.nextDouble();
        
        System.out.print("Annual Interest Rate: ");
        double annInterest = scanner.nextDouble();
        
        System.out.print("Period (years): ");
        double periodY = scanner.nextDouble();
        
        double monInterest = (annInterest/MONTHS)/PERCENTAGE;
        double periodM = periodY*MONTHS;
        double n = Math.pow((1 + monInterest) ,periodM);
        double mortgage = principal*((monInterest*n)/(n-1));
        String formatMortgage = NumberFormat.getCurrencyInstance().format(mortgage);
        
        System.out.println("Mortgage: " + formatMortgage);
        
        
    }
}

I was using println() before and the problem didn't occur. I also tried moving some stuff around but still the same issue..

java

string

math

input

java.util.scanner

0 Answers

Your Answer

Accepted video resources