I'm working on a program that asks for some numbers from the user then does a calculation based on input. It appears that each path works (calculating monthly payment or calculating principal amount) but in order to see the calculated amount I must press enter after I input the number of monthly payments. Can anybody see why this is happening? Thanks, Michele
/**
* Write a description of class MonthlyPayment here.
* Added 8/9/10: Prompts user to determine what kind of calculation should be done; monthly payment or principle
* Prompts user to enter house cost, down payment, interest rate and number of payments then returns the monthly payment amount.
* Prompts user to enter monthly payment, down payment, interest rate and number of payments then returns the principle amount.
* Added 8/16/10: Repeats until Q is entered
* Added 8/16/10: Verifies valid values for M and P paths
* @Michele
* @3.0
*/
import java.util.Scanner;
import java.text.*;
publicclass MonthlyPayment
{
// variables passed to calc function
privatedouble houseCost;
privateint downPayment;
privatedouble rate;
privateint numOfPayments;
privatedouble monthlyPayment;
publicstaticvoid main(String [] args)
{
char inputType;
Scanner keyInput = new Scanner(System.in);
System.out.print("Enter 'P' to calculate principle, 'M' to calculate monthly payment or 'Q' to quit: ");
inputType = keyInput.next().charAt(0);
int inputHouse = 0;
int inputMonthlyPayment = 0;
int inputDown = 0;
double inputRate = 0;
int inputNum = 0;
MonthlyPayment mp = new MonthlyPayment ();
while (inputType != 'Q' && inputType != 'q')
{
if (inputType != 'M' && inputType != 'm' && inputType != 'P' && inputType != 'p')
{
System.out.println("Please enter a valid value 'M', 'P' or 'Q': ");
inputType = keyInput.next().charAt(0);
keyInput.nextLine();
}
if (inputType == 'M' || inputType == 'm')
{
do
{
System.out.print("Enter the house cost: ");
if (keyInput.hasNextInt())
{
inputHouse = keyInput.nextInt();
}
keyInput.nextLine();
if (inputHouse <= 0)
{
System.out.println("invalid value");
}
} // end do
while (inputHouse <= 0);
} //end if m
if (inputType == 'P' || inputType == 'p')
{
do
{
System.out.print("Enter the monthly payment: ");
if (keyInput.hasNextInt())
{
inputMonthlyPayment = keyInput.nextInt();
}
if (inputMonthlyPayment < 0 || inputMonthlyPayment > 999999)
{
System.out.println("Invalid value");
}
} // end do
while (inputMonthlyPayment < 0 || inputMonthlyPayment > 999999);
} //end if p
do
{
System.out.print("Enter the down payment: ");
if (keyInput.hasNextInt())
{
inputDown = keyInput.nextInt();
}
keyInput.nextLine();
if (inputDown <= 0 || inputDown > inputHouse)
{
System.out.println("invalid value");
}
} // end do
while (inputDown <= 0 || inputDown > inputHouse);
do
{
System.out.print("Enter the interest rate: (ex 7.5) ");
if (keyInput.hasNextDouble())
{
inputRate = keyInput.nextDouble();
}
keyInput.nextLine();
if (inputRate < 0 || inputRate > 100)
{
System.out.println("Invalid value");
}
} // end do
while (inputRate < 0 || inputRate > 100);
do
{
System.out.print("Enter the number of monthly payments: ");
if (keyInput.hasNextInt())
{
inputNum = keyInput.nextInt();
}
keyInput.nextLine();
if (inputNum < 0 || inputNum > 99999)
{
System.out.println("Invalid value");
}
else
{
keyInput.nextLine();
}
} // end do
while (inputNum < 0 || inputNum > 99999);
if (inputType == 'M' || inputType == 'm')
{
// Defines new decimal format using java.text.* class
DecimalFormat twoDigit = new DecimalFormat("#,##0.00");//formats to 2 decimal places
mp.setHouseCost(inputHouse);
mp.setDownPayment(inputDown);
mp.setRate(inputRate);
mp.setNumOfPayments(inputNum);
mp.calcPayment();
// Returns Monthly payment formatted to 2 decimal places
System.out.println("Your monthly payment is: $" + twoDigit.format(mp.getMonthlyPayment()));
}
if (inputType == 'P' || inputType == 'p')
{
mp.setDownPayment(inputDown);
mp.setRate(inputRate);
mp.setNumOfPayments(inputNum);
mp.setPayment(inputMonthlyPayment);
mp.calcPrinciple();
// Defines new decimal format using java.text.* class
DecimalFormat twoDigit = new DecimalFormat("#,##0.00");//formats to 2 decimal places
// Returns Principle amount formatted to 2 decimal places
System.out.println("Your principle amount is: $" + twoDigit.format(mp.getPrinciple()));
} // ends if
System.out.print("Enter 'P' to calculate principle, 'M' to calculate monthly payment or 'Q' to quit: ");
inputType = keyInput.next().charAt(0);
} //End while !q
} // end main
public MonthlyPayment()
{// Initialize variables
houseCost = 0;
downPayment = 0;
rate = 0.00;
numOfPayments = 0;
monthlyPayment = 0.00;
}
publicvoid calcPayment () // void because it doesn't return value, calculates payment
{
monthlyPayment = (((houseCost - downPayment)*(.01*rate/12))/(1-Math.pow((1+(.01*rate/12)),-(numOfPayments))));
}
publicvoid calcPrinciple () // void because it doesn't return value, calculates principle amount
{
houseCost = downPayment+(12*monthlyPayment)*(1-Math.pow((1+(.01*rate/12)),-(numOfPayments)))/(.01*rate);
}
publicvoid setHouseCost (int costIn) // sets user entered value to costIn
{
houseCost = costIn;
}
publicvoid setDownPayment (int downPayIn) // sets user entered value to downPayIn
{
downPayment = downPayIn;
}
publicvoid setRate (double rateIn) // sets user entered value to rateIn
{
rate = rateIn;
}
publicvoid setNumOfPayments (int numOfPaymentsIn) // sets user entered value to numOfPaymentsIn
{
numOfPayments = numOfPaymentsIn;
}
publicvoid setPayment (double paymentIn) // sets user entered value to paymentIn
{
monthlyPayment = paymentIn;
}
publicdouble getMonthlyPayment () // returns the calculated monthly payment
{
return monthlyPayment;
}
publicdouble getPrinciple () // returns the calculated priciple amt
{
return houseCost;
}
} // ends class
your program code is querying input from the generic system InputStream called System.in
No input appears in the stream until a carriage return is entered from the system input device, in this case a keyboard from a simulated DOS shell. This is obviously not a normal way of getting input in a client application in today's programming world, but it still works fine.
To get actions to occur for specific key presses, you need to obtain input from a GUI which have a KeyListener added.