User Input without GUI... Need Guidance

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2007
Posts: 7
Reputation: ajcornwell is an unknown quantity at this point 
Solved Threads: 0
ajcornwell ajcornwell is offline Offline
Newbie Poster

User Input without GUI... Need Guidance

 
0
  #1
Sep 26th, 2007
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.


  1.  
  2.  
  3. /*
  4.   MortgageProgramWK4b.java
  5. This program will accept user input for the loan amount,
  6. interest rate, and term of the loan. Then calculate and
  7. display the monthly mortgage payment amount, Followed by,
  8. listing the loan balance and interest paid for
  9.   each payment over the term of the loan.
  10. */
  11.  
  12. import java.math.*;
  13. import java.io.*;
  14.  
  15. public static void main(String[] args)throws Exception
  16. {
  17.  
  18. //local objects
  19. MortgageProgramWK4b mortgageCalc = new MortgageWK4b();
  20. DecimalFormat df = new DecimalFormat("###,##0.00");
  21. DecimalFormat numDf = new DecimalFormat("000");
  22.  
  23. int numOfYears;
  24. double annualInterest;
  25. double principal;
  26. double monthlyPayment;
  27.  
  28. //Display Title
  29. System.out.println("Mortgage Calculator");
  30. System.out.println();
  31.  
  32. //Get User Input of principal
  33. int principal = readInterger("Please enter the pricipal amount of the loan." );
  34.  
  35. //Get User Input of rate
  36. int annualInterest = readInterger("Please enter the pricipal amount of the loan." );
  37.  
  38. //Get User Input of Term
  39. int numOfYears = readInterger("Please enter the pricipal amount of the loan." );
  40.  
  41.  
  42. double monthlyInterest = annualInterest/(12*100);!
  43. int totalNumOfMonths = numOfYears*12;
  44. System.out.println("Payment# LoanBalance InterestPaid");
  45. int monthCounter=1;
  46. for(; totalNumOfMonths>0; totalNumOfMonths--){
  47. //first get the monthly payment...
  48. double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths);
  49.  
  50. double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal);
  51. principal-=monthlyPrincipal;
  52. System.out.println(numDf.format(monthCounter) + " " +df.format(principal)+ " " +df.format((monthlyPayment-monthlyPrincipal)));
  53. monthCounter++;
  54. if(monthCounter%25==0)sleep(1000); // this will hesitate...
  55. }
  56. }
  57.  
  58.  
  59.  
  60.  
  61. public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){
  62. // Monthly payment formula: M = P x (J/(1-(1+J)^-N));
  63. // M = P * ( J / (1 - (1 + J) ** -N));
  64. // source: http://www.hughchou.org/calc/formula.html
  65. double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
  66. return monthlyPayment;
  67. }
  68.  
  69. //calculate the monthly interst using simple interest.
  70. public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){
  71. return monthlyPayment - (remainingPrincipal * monthlyInterest);
  72. }
  73.  
  74. // method to make the program sleep (hesitate) for a give time (in milliseconds)..
  75. public static void sleep(long milliseconds){
  76. try{
  77. Thread.sleep(milliseconds);
  78. }catch(Exception e){
  79.  
  80. }
  81. }
  82.  
  83. }
_____________________________

This is the other way I tried it... I am getting the same three errors both ways...

  1.  
  2. /*
  3.   MortgageProgramWK4a.java
  4. This program will accept user input for the loan amount,
  5. interest rate, and term of the loan. Then calculate and
  6. display the monthly mortgage payment amount, Followed by,
  7. listing the loan balance and interest paid for
  8.   each payment over the term of the loan.
  9. */
  10.  
  11. import java.math.*;
  12. import java.io.*;
  13.  
  14. public static void main(String[] args)throws Exception
  15. {
  16.  
  17. //local objects
  18. MortgageProgramWK4a mortgageCalc = new MortgageProgramWK4a();
  19. DecimalFormat df = new DecimalFormat("###,##0.00");
  20. DecimalFormat numDf = new DecimalFormat("000");
  21.  
  22. int numOfYears;
  23. double annualInterest;
  24. double principal;
  25. double monthlyPayment;
  26.  
  27.  
  28. //Display Title
  29. System.out.println("Mortgage Calculator");
  30. System.out.println();
  31.  
  32. //Get User Input of principal
  33. System.out.println("Please enter the loan amount: ");
  34. principal = (double)System.in.read();System.in.read();System.in.read();
  35.  
  36. //Get User Input of rate
  37. System.out.println("Please enter the interest rate: ");
  38. annualInterest= (int)System.in.read();System.in.read();System.in.read();
  39.  
  40. //Get User Input of Term
  41. System.out.println("Please enter the term of the loan in years: ");
  42. numOfYears = (int)System.in.read();System.in.read();System.in.read();
  43.  
  44. int numOfYears = System.in();
  45. double monthlyInterest = annualInterest/(12*100);!
  46. int totalNumOfMonths = numOfYears*12;
  47. System.out.println("Payment# LoanBalance InterestPaid");
  48. int monthCounter=1;
  49. for(; totalNumOfMonths>0; totalNumOfMonths--){
  50. //first get the monthly payment...
  51. double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths);
  52. //now get the monthly principal in the payment...
  53. double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal);
  54. principal-=monthlyPrincipal;
  55.  
  56. System.out.println(numDf.format(monthCounter) + " " +df.format(principal)+ " " +df.format((monthlyPayment-monthlyPrincipal)));
  57. monthCounter++;
  58. if(monthCounter%25==0)sleep(1000); // this will hesitate...
  59. }
  60. }
  61.  
  62.  
  63.  
  64. public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){
  65. // Monthly payment formula: M = P x (J/(1-(1+J)^-N));
  66. // M = P * ( J / (1 - (1 + J) ** -N));
  67. // source: http://www.hughchou.org/calc/formula.html
  68. double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
  69. return monthlyPayment;
  70. }
  71.  
  72. //calculate the monthly interst using simple interest.
  73. public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){
  74. return monthlyPayment - (remainingPrincipal * monthlyInterest);
  75. }
  76.  
  77. // method to make the program sleep (hesitate) for a give time (in milliseconds)..
  78. public static void sleep(long milliseconds){
  79. try{
  80. Thread.sleep(milliseconds);
  81. }catch(Exception e){
  82.  
  83. }
  84. }
  85.  
  86. }


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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: User Input without GUI... Need Guidance

 
0
  #2
Sep 26th, 2007
Well, you don't define a class at all - just a main() method. Methods can't exist on their own, they must be in a class. You also don't define the readInterger (sic, misspelled Integer) method either, so you can't call it until you define it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 7
Reputation: ajcornwell is an unknown quantity at this point 
Solved Threads: 0
ajcornwell ajcornwell is offline Offline
Newbie Poster

Re: User Input without GUI... Need Guidance

 
0
  #3
Sep 27th, 2007
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?

  1.  
  2. /*
  3.   MortgageProgramWK4b.java
  4. This program will accept user input for the loan amount,
  5. interest rate, and term of the loan. Then calculate and
  6. display the monthly mortgage payment amount, Followed by,
  7. listing the loan balance and interest paid for
  8.   each payment over the term of the loan.
  9. */
  10.  
  11. import java.math.*;
  12. import java.io.*;
  13.  
  14. class MortgageProgramWK4b
  15. {
  16. public static void main(String[] args)throws Exception
  17.  
  18. {
  19. public boolean ReadInteger(String Key, String ValueName, int DefValue,Integer Value);
  20. {
  21. //local objects
  22. MortgageProgramWK4b mortgageCalc = new MortgageWK4b();
  23. DecimalFormat df = new DecimalFormat("###,##0.00");
  24. DecimalFormat numDf = new DecimalFormat("000");
  25.  
  26. int numOfYears;
  27. double annualInterest;
  28. double principal;
  29. double monthlyPayment;
  30.  
  31. //Display Title
  32. System.out.println("Mortgage Calculator");
  33. System.out.println();
  34.  
  35. //Get User Input of principal
  36. int principal = readInterger("Please enter the pricipal amount of the loan." );
  37.  
  38. //Get User Input of rate
  39. int annualInterest = readInterger("Please enter the pricipal amount of the loan." );
  40.  
  41. //Get User Input of Term
  42. int numOfYears = readInterger("Please enter the pricipal amount of the loan." );
  43.  
  44.  
  45. double monthlyInterest = annualInterest/(12*100);!
  46. int totalNumOfMonths = numOfYears*12;
  47. System.out.println("Payment# LoanBalance InterestPaid");
  48. int monthCounter=1;
  49. for(; totalNumOfMonths>0; totalNumOfMonths--){
  50. //first get the monthly payment...
  51. double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths);
  52.  
  53. double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal);
  54. principal-=monthlyPrincipal;
  55. System.out.println(numDf.format(monthCounter) + " " +df.format(principal)+ " " +df.format((monthlyPayment-monthlyPrincipal)));
  56. monthCounter++;
  57. if(monthCounter%25==0)sleep(1000); // this will hesitate...
  58. }
  59. }
  60.  
  61.  
  62.  
  63.  
  64. public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){
  65. // Monthly payment formula: M = P x (J/(1-(1+J)^-N));
  66. // M = P * ( J / (1 - (1 + J) ** -N));
  67. // source: http://www.hughchou.org/calc/formula.html
  68. double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
  69. return monthlyPayment;
  70. }
  71.  
  72. //calculate the monthly interst using simple interest.
  73. public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){
  74. return monthlyPayment - (remainingPrincipal * monthlyInterest);
  75. }
  76.  
  77. // method to make the program sleep (hesitate) for a give time (in milliseconds)..
  78. public static void sleep(long milliseconds){
  79. try{
  80. Thread.sleep(milliseconds);
  81. }catch(Exception e){
  82.  
  83. }
  84. }
  85. ;
  86. }}
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: User Input without GUI... Need Guidance

 
0
  #4
Sep 27th, 2007
You cannot define methods within other methods, which is where you are trying to put ReadInteger. You have it declared in main(). Also, readInteger() should read an int input, which you must write the code for - just writing a declaration line won't do anything.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 164
Reputation: orko is an unknown quantity at this point 
Solved Threads: 10
orko orko is offline Offline
Junior Poster

Re: User Input without GUI... Need Guidance

 
0
  #5
Sep 28th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 7
Reputation: ajcornwell is an unknown quantity at this point 
Solved Threads: 0
ajcornwell ajcornwell is offline Offline
Newbie Poster

Re: User Input without GUI... Need Guidance

 
0
  #6
Sep 28th, 2007
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....
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: User Input without GUI... Need Guidance

 
0
  #7
Sep 28th, 2007
Originally Posted by ajcornwell View Post
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....
He was trying to say that your method needs to be separate from main - not written inside it.
  1. public class MyClass{
  2.  
  3. public static void main(String[] args){
  4. someMethod();
  5. }
  6.  
  7. public static void someMethod(){
  8. // do some stuff
  9. }
  10. }
Last edited by Ezzaral; Sep 28th, 2007 at 4:27 pm. Reason: added example
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 7
Reputation: ajcornwell is an unknown quantity at this point 
Solved Threads: 0
ajcornwell ajcornwell is offline Offline
Newbie Poster

Re: User Input without GUI... Need Guidance

 
0
  #8
Sep 30th, 2007
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!

  1. /*
  2.   MortgageProgram.java
  3. This program will accept user input for the loan amount,
  4. interest rate, and term of the loan. Then calculate and
  5. display the monthly mortgage payment amount, Followed by,
  6. listing the loan balance and interest paid for
  7.   each payment over the term of the loan.
  8.  
  9. */
  10.  
  11. import java.io.*;
  12. import java.text.*;
  13.  
  14.  
  15. public class MortgageProgram{
  16.  
  17.  
  18. private static BufferedReader stdin = new BufferedReader(
  19. new InputStreamReader (System.in));
  20.  
  21.  
  22. public static void main(String[] args) throws IOException{
  23. //local objects
  24. MortgageWK3 mortgageCalc = new MortgageWK3();
  25. DecimalFormat df = new DecimalFormat("###,##0.00"); // to format the amount.
  26. DecimalFormat numDf = new DecimalFormat("000"); // to format the sequence number.
  27.  
  28. double principal = 0;
  29. double annualInterest = 0.0;
  30. int numOfYears = 0;
  31.  
  32. System.out.print("Please enter the amount borrowed:"); //prompt user input
  33. String input = stdin.readLine(); //get user input
  34. principal= Integer.parseInt(input); //convert string to int
  35.  
  36. System.out.print("Please enter the interest rate:");
  37. String input2 = stdin.readLine();
  38. annualInterest = Integer.parseInt(input2);
  39.  
  40. System.out.print("Please enter the term of the loan in years:");
  41. String input3 = stdin.readLine();
  42. numOfYears = Integer.parseInt(input3);
  43.  
  44. //variables
  45. double monthlyInterest = annualInterest/(12*100);
  46. int totalNumOfMonths = numOfYears*12;
  47.  
  48.  
  49. System.out.println("Payment# LoanBalance InterestPaid");
  50. int monthCounter=1;
  51. for(; totalNumOfMonths>0; totalNumOfMonths--){
  52. //first get the monthly payment...
  53. double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths);
  54.  
  55. //now get the monthly principal in the payment...
  56. double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal);
  57. principal-=monthlyPrincipal;
  58.  
  59. System.out.println(numDf.format(monthCounter) + " " +df.format(principal)+ " " +df.format((monthlyPayment-monthlyPrincipal)));
  60. monthCounter++;
  61. if(monthCounter%25==0)sleep(1000); // this will hesitate...
  62.  
  63.  
  64. }
  65.  
  66. }
  67.  
  68. // this is from our last week.. All I did is move it to a function and give it a name
  69. // the getMortgageAmount is the function name taking in the 3 arguments.
  70. public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){
  71. // Monthly payment formula: M = P x (J/(1-(1+J)^-N));
  72. // M = P * ( J / (1 - (1 + J) ** -N));
  73. // source: http://www.hughchou.org/calc/formula.html
  74. double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
  75. return monthlyPayment;
  76.  
  77. }
  78.  
  79. //calculate the monthly interst using simple interest.
  80. public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){
  81. return monthlyPayment - (remainingPrincipal * monthlyInterest);
  82. }
  83.  
  84. // method to make the program sleep (hesitate) for a give time (in milliseconds)..
  85. public static void sleep(long milliseconds){
  86. try{
  87. Thread.sleep(milliseconds);
  88. }catch(Exception e){
  89. //do nothing... for now...
  90. }
  91. }
  92.  
  93. }


Thank again for all the input from before, y'alls help has been invaluable! : )
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 7
Reputation: ajcornwell is an unknown quantity at this point 
Solved Threads: 0
ajcornwell ajcornwell is offline Offline
Newbie Poster

Re: User Input without GUI... Need Guidance

 
0
  #9
Oct 1st, 2007
I have solved the decimal problem, I still haven't been able to figure out how to get the MonthlyPayment to print only once, before the loop, without being in the loop to print over and over again.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: User Input without GUI... Need Guidance

 
0
  #10
Oct 1st, 2007
Just move the monthly payment call out of your loop and print it before the rest. Having it in the loop, you are actually changing the monthly payment on each iteration, which I don't believe you want to do.
Last edited by Ezzaral; Oct 1st, 2007 at 1:41 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC