| | |
User Input without GUI... Need Guidance
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2007
Posts: 7
Reputation:
Solved Threads: 0
I am stuck again, I was able to complete last week's assignment without having to post, just so you guys don't think I'm a lost cause!
I need to allow the user to input loan amount, term, and interest rate before calculating the monthly payment and showing the table. This is what I have so far. I am getting between two and three errors that I cannot figure out. Any help and/or hints are greatly appreciated.
_____________________________
This is the other way I tried it... I am getting the same three errors both ways...
I am getting the same three errors, all of them are saying "class" or "interface" expected. Again, any help is really appreciated.
I need to allow the user to input loan amount, term, and interest rate before calculating the monthly payment and showing the table. This is what I have so far. I am getting between two and three errors that I cannot figure out. Any help and/or hints are greatly appreciated. Java Syntax (Toggle Plain Text)
/* MortgageProgramWK4b.java This program will accept user input for the loan amount, interest rate, and term of the loan. Then calculate and display the monthly mortgage payment amount, Followed by, listing the loan balance and interest paid for each payment over the term of the loan. */ import java.math.*; import java.io.*; public static void main(String[] args)throws Exception { //local objects MortgageProgramWK4b mortgageCalc = new MortgageWK4b(); DecimalFormat df = new DecimalFormat("###,##0.00"); DecimalFormat numDf = new DecimalFormat("000"); int numOfYears; double annualInterest; double principal; double monthlyPayment; //Display Title System.out.println("Mortgage Calculator"); System.out.println(); //Get User Input of principal int principal = readInterger("Please enter the pricipal amount of the loan." ); //Get User Input of rate int annualInterest = readInterger("Please enter the pricipal amount of the loan." ); //Get User Input of Term int numOfYears = readInterger("Please enter the pricipal amount of the loan." ); double monthlyInterest = annualInterest/(12*100);! int totalNumOfMonths = numOfYears*12; System.out.println("Payment# LoanBalance InterestPaid"); int monthCounter=1; for(; totalNumOfMonths>0; totalNumOfMonths--){ //first get the monthly payment... double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths); double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal); principal-=monthlyPrincipal; System.out.println(numDf.format(monthCounter) + " " +df.format(principal)+ " " +df.format((monthlyPayment-monthlyPrincipal))); monthCounter++; if(monthCounter%25==0)sleep(1000); // this will hesitate... } } public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){ // Monthly payment formula: M = P x (J/(1-(1+J)^-N)); // M = P * ( J / (1 - (1 + J) ** -N)); // source: http://www.hughchou.org/calc/formula.html double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1)))))); return monthlyPayment; } //calculate the monthly interst using simple interest. public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){ return monthlyPayment - (remainingPrincipal * monthlyInterest); } // method to make the program sleep (hesitate) for a give time (in milliseconds).. public static void sleep(long milliseconds){ try{ Thread.sleep(milliseconds); }catch(Exception e){ } } }
This is the other way I tried it... I am getting the same three errors both ways...
Java Syntax (Toggle Plain Text)
/* MortgageProgramWK4a.java This program will accept user input for the loan amount, interest rate, and term of the loan. Then calculate and display the monthly mortgage payment amount, Followed by, listing the loan balance and interest paid for each payment over the term of the loan. */ import java.math.*; import java.io.*; public static void main(String[] args)throws Exception { //local objects MortgageProgramWK4a mortgageCalc = new MortgageProgramWK4a(); DecimalFormat df = new DecimalFormat("###,##0.00"); DecimalFormat numDf = new DecimalFormat("000"); int numOfYears; double annualInterest; double principal; double monthlyPayment; //Display Title System.out.println("Mortgage Calculator"); System.out.println(); //Get User Input of principal System.out.println("Please enter the loan amount: "); principal = (double)System.in.read();System.in.read();System.in.read(); //Get User Input of rate System.out.println("Please enter the interest rate: "); annualInterest= (int)System.in.read();System.in.read();System.in.read(); //Get User Input of Term System.out.println("Please enter the term of the loan in years: "); numOfYears = (int)System.in.read();System.in.read();System.in.read(); int numOfYears = System.in(); double monthlyInterest = annualInterest/(12*100);! int totalNumOfMonths = numOfYears*12; System.out.println("Payment# LoanBalance InterestPaid"); int monthCounter=1; for(; totalNumOfMonths>0; totalNumOfMonths--){ //first get the monthly payment... double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths); //now get the monthly principal in the payment... double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal); principal-=monthlyPrincipal; System.out.println(numDf.format(monthCounter) + " " +df.format(principal)+ " " +df.format((monthlyPayment-monthlyPrincipal))); monthCounter++; if(monthCounter%25==0)sleep(1000); // this will hesitate... } } public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){ // Monthly payment formula: M = P x (J/(1-(1+J)^-N)); // M = P * ( J / (1 - (1 + J) ** -N)); // source: http://www.hughchou.org/calc/formula.html double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1)))))); return monthlyPayment; } //calculate the monthly interst using simple interest. public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){ return monthlyPayment - (remainingPrincipal * monthlyInterest); } // method to make the program sleep (hesitate) for a give time (in milliseconds).. public static void sleep(long milliseconds){ try{ Thread.sleep(milliseconds); }catch(Exception e){ } } }
I am getting the same three errors, all of them are saying "class" or "interface" expected. Again, any help is really appreciated.
Last edited by ajcornwell; Sep 26th, 2007 at 2:14 pm.
•
•
Join Date: Sep 2007
Posts: 7
Reputation:
Solved Threads: 0
I feel like a goober for forgetting the class. I tried to define the readInteger() but I still can't make this work. Any other suggestions?
Java Syntax (Toggle Plain Text)
/* MortgageProgramWK4b.java This program will accept user input for the loan amount, interest rate, and term of the loan. Then calculate and display the monthly mortgage payment amount, Followed by, listing the loan balance and interest paid for each payment over the term of the loan. */ import java.math.*; import java.io.*; class MortgageProgramWK4b { public static void main(String[] args)throws Exception { public boolean ReadInteger(String Key, String ValueName, int DefValue,Integer Value); { //local objects MortgageProgramWK4b mortgageCalc = new MortgageWK4b(); DecimalFormat df = new DecimalFormat("###,##0.00"); DecimalFormat numDf = new DecimalFormat("000"); int numOfYears; double annualInterest; double principal; double monthlyPayment; //Display Title System.out.println("Mortgage Calculator"); System.out.println(); //Get User Input of principal int principal = readInterger("Please enter the pricipal amount of the loan." ); //Get User Input of rate int annualInterest = readInterger("Please enter the pricipal amount of the loan." ); //Get User Input of Term int numOfYears = readInterger("Please enter the pricipal amount of the loan." ); double monthlyInterest = annualInterest/(12*100);! int totalNumOfMonths = numOfYears*12; System.out.println("Payment# LoanBalance InterestPaid"); int monthCounter=1; for(; totalNumOfMonths>0; totalNumOfMonths--){ //first get the monthly payment... double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths); double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal); principal-=monthlyPrincipal; System.out.println(numDf.format(monthCounter) + " " +df.format(principal)+ " " +df.format((monthlyPayment-monthlyPrincipal))); monthCounter++; if(monthCounter%25==0)sleep(1000); // this will hesitate... } } public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){ // Monthly payment formula: M = P x (J/(1-(1+J)^-N)); // M = P * ( J / (1 - (1 + J) ** -N)); // source: http://www.hughchou.org/calc/formula.html double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1)))))); return monthlyPayment; } //calculate the monthly interst using simple interest. public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){ return monthlyPayment - (remainingPrincipal * monthlyInterest); } // method to make the program sleep (hesitate) for a give time (in milliseconds).. public static void sleep(long milliseconds){ try{ Thread.sleep(milliseconds); }catch(Exception e){ } } ; }}
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
main has to be seperate function similar to your other functions... well here is the go.... main is also a regular function like your other function... but we have to write main function for something to begin with... your machine can compile all the functions... but if it finds main function, it takes that as a starting point.... any other methods you want to use, has to be linked with main by calling the function directly in main or in some other function, which was called in main.
A Perfect World
•
•
•
•
How would I call the function in main without defining it as a method? I think I'm missing a pretty large chuck of the puzzle....
Java Syntax (Toggle Plain Text)
public class MyClass{ public static void main(String[] args){ someMethod(); } public static void someMethod(){ // do some stuff } }
Last edited by Ezzaral; Sep 28th, 2007 at 4:27 pm. Reason: added example
•
•
Join Date: Sep 2007
Posts: 7
Reputation:
Solved Threads: 0
Okay, thanks for the help guys. I was really frustrated, therefore I started over from the begining. The good news is that I have it working... however, I need to be able to input a decimal for the annual interest input, if that is input it freaks out and quits, if a whole number is entered it works fine.
The only other issue I need to fix, it that I need to print the Montly Payment. I can make it print but I don't want it in the loop, because it prints over and over obviously. I can make it print to the screen anywhere else! Help!
Thank again for all the input from before, y'alls help has been invaluable! : )
The only other issue I need to fix, it that I need to print the Montly Payment. I can make it print but I don't want it in the loop, because it prints over and over obviously. I can make it print to the screen anywhere else! Help!
Java Syntax (Toggle Plain Text)
/* MortgageProgram.java This program will accept user input for the loan amount, interest rate, and term of the loan. Then calculate and display the monthly mortgage payment amount, Followed by, listing the loan balance and interest paid for each payment over the term of the loan. */ import java.io.*; import java.text.*; public class MortgageProgram{ private static BufferedReader stdin = new BufferedReader( new InputStreamReader (System.in)); public static void main(String[] args) throws IOException{ //local objects MortgageWK3 mortgageCalc = new MortgageWK3(); DecimalFormat df = new DecimalFormat("###,##0.00"); // to format the amount. DecimalFormat numDf = new DecimalFormat("000"); // to format the sequence number. double principal = 0; double annualInterest = 0.0; int numOfYears = 0; System.out.print("Please enter the amount borrowed:"); //prompt user input String input = stdin.readLine(); //get user input principal= Integer.parseInt(input); //convert string to int System.out.print("Please enter the interest rate:"); String input2 = stdin.readLine(); annualInterest = Integer.parseInt(input2); System.out.print("Please enter the term of the loan in years:"); String input3 = stdin.readLine(); numOfYears = Integer.parseInt(input3); //variables double monthlyInterest = annualInterest/(12*100); int totalNumOfMonths = numOfYears*12; System.out.println("Payment# LoanBalance InterestPaid"); int monthCounter=1; for(; totalNumOfMonths>0; totalNumOfMonths--){ //first get the monthly payment... double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths); //now get the monthly principal in the payment... double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal); principal-=monthlyPrincipal; System.out.println(numDf.format(monthCounter) + " " +df.format(principal)+ " " +df.format((monthlyPayment-monthlyPrincipal))); monthCounter++; if(monthCounter%25==0)sleep(1000); // this will hesitate... } } // this is from our last week.. All I did is move it to a function and give it a name // the getMortgageAmount is the function name taking in the 3 arguments. public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){ // Monthly payment formula: M = P x (J/(1-(1+J)^-N)); // M = P * ( J / (1 - (1 + J) ** -N)); // source: http://www.hughchou.org/calc/formula.html double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1)))))); return monthlyPayment; } //calculate the monthly interst using simple interest. public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){ return monthlyPayment - (remainingPrincipal * monthlyInterest); } // method to make the program sleep (hesitate) for a give time (in milliseconds).. public static void sleep(long milliseconds){ try{ Thread.sleep(milliseconds); }catch(Exception e){ //do nothing... for now... } } }
Thank again for all the input from before, y'alls help has been invaluable! : )
![]() |
Similar Threads
- user input into a string (C++)
- Tutorial: User Input: Strings and Numbers [C] (C)
- inline assembly getting user input.(djgpp) (Assembly)
- Type Conversion of User Input: (C++)
- who can help me with GUI?? (Java)
- Error Checking for user input (Java)
- error checking of user input (C++)
- Creating a GUI that accepts user input help (Java)
- Need Help With Error Checking User Input (C)
- filtering bad user input (Java)
Other Threads in the Java Forum
- Previous Thread: Bizzare logic error
- Next Thread: what happen during program setup??
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






