The Artima Developer Community
Sponsored Link

Java Answers Forum
Not sure why I have to press enter to get results

1 reply on 1 page. Most recent reply: Sep 4, 2010 8:18 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Michele Atkinson

Posts: 1
Nickname: mla56
Registered: Sep, 2010

Not sure why I have to press enter to get results Posted: Sep 3, 2010 6:10 PM
Reply to this message Reply
Advertisement
Hi,

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.*; 
public class MonthlyPayment
{
    // variables passed to calc function
    private double houseCost;
    private int downPayment;
    private double rate;
    private int numOfPayments;
    private double monthlyPayment;
       
  public static void 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;
    }
  public void calcPayment () // void because it doesn't return value, calculates payment
  {
    monthlyPayment = (((houseCost - downPayment)*(.01*rate/12))/(1-Math.pow((1+(.01*rate/12)),-(numOfPayments))));
  }
  public void 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); 
  }   
  public void setHouseCost (int costIn) // sets user entered value to costIn
  {
     houseCost = costIn;
  }
  public void setDownPayment (int downPayIn) // sets user entered value to downPayIn
  {
    downPayment = downPayIn;
  }
  public void setRate (double rateIn) // sets user entered value to rateIn
  {
    rate = rateIn;
  }
  public void setNumOfPayments (int numOfPaymentsIn) // sets user entered value to numOfPaymentsIn
  {
    numOfPayments = numOfPaymentsIn;
  }
  public void setPayment (double paymentIn) // sets user entered value to paymentIn
  {
    monthlyPayment = paymentIn;
  }   
  public double getMonthlyPayment () // returns the calculated monthly payment
  {
    return monthlyPayment;
  }
  public double getPrinciple () // returns the calculated priciple amt
  {
    return houseCost;
  }
} // ends class


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Not sure why I have to press enter to get results Posted: Sep 4, 2010 8:18 AM
Reply to this message Reply
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.

Flat View: This topic has 1 reply on 1 page
Topic: HELP! Design Review & Risk Management topics (in Java projects) required Previous Topic   Next Topic Topic: java code

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use