# Thread: need help checking monthlyRate entry

1. Member Join Date
Nov 2007
Location
boston
Posts
10
Rep Power
0

## need help checking monthlyRate entry

how can i calculate the monthlyRate if user enter any #.exempl 9 or .09 or 15 or .15.rite now it works with any decimal and will give me the rite answer.how can i check for both #(allnumber and decimal) them calculate and get rite answer? i like to know thnks

import java.util.Scanner;

class C5E7experc{

public static void main(String [] args){

Scanner input = new Scanner(System.in);
int years;
double futureInvestementValue;
double investmentAmount;
double interestRate;
double result;

System.out.println(" enter amount invest:");
investmentAmount = input.nextDouble();

System.out.println(" enter monthly Rate ");
interestRate = input.nextDouble();

System.out.println("THE AMOUNT INVESTED IS:" + investmentAmount);
System.out.println("THE ANNUAL INTEREST RATE IS:" + interestRate);
System.out.println("YEARS\t\tFUTURE VALUE");
for (int i = 1; i <= 30; ++i)
{

result = futureInvestmentValue( investmentAmount, interestRate/12, i);
System.out.println(i + "\t\t" +"\$" + result);
/*
* System.out.print(i + "\t"); //another way to format
* Sytem.out.printf("\$%8.2f" , result);
* System.out.println(); */
}// end for

}//end main

public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years){

double netInvestmentAmount;
netInvestmentAmount = investmentAmount *
Math.pow (1 + monthlyInterestRate, years * 12) ;
netInvestmentAmount = (int) (netInvestmentAmount * 100) / 100.0;

return netInvestmentAmount;
} //end futureInvestmentValue

}// end class  Reply With Quote

2. ## Java Code:
```        System.out.println(" enter amount invest:");
investmentAmount = input.nextDouble();
//        investmentAmount = Double.parseDouble(input.nextLine());

System.out.println(" enter monthly Rate ");
if(input.hasNextInt())
interestRate = input.nextDouble()/100.0;
else
interestRate = input.nextDouble();
/*
// Using nextLine here required it be used for investmentAmount.
String line = input.nextLine();
interestRate = Double.parseDouble(line);
if(line.indexOf(".") == -1)
interestRate /= 100.0;
*/```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•