We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Java array mortgage calculator user input and variable define issue

marchbaby
marchbaby asked
on
Medium Priority
645 Views
Last Modified: 2012-05-11
I am new to java and to working with arrays. I have to change my code to using an array to store user input. I have defined the array in the beginning, but now the user input states "already defined in main" due to the array declaration.  I'm at a loss what to change for the user input so it stores in the array. The first name, last name, loan amount, interest rate and term years all have to be stored in an array. I would be very grateful for any help.


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tryingagain;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


//Author 

import java.io.*;
import java.text.*;
import java.lang.String;
import java.util.Scanner;
import java.util.Arrays;



public class Main {
  public static void main(String[] args) throws IOException {


   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

   String [] fname;
   String [] lname;
   double [] loanAmount;
   double [] rateYears;
   int [] termYears;


    System.out.println("\t\t\tWelcome to yet another Mortgage Calculator\n");
      {
        System.out.println("\tPlease enter your first name:");
        String fname  = in.readLine();
        while (fname.equals(""))
        {
            System.out.println("\tName error! Please enter your first name: ");
            fname = in.readLine();
        }
      }
        System.out.println("\tPlease enter your last name:");
       String lname = in.readLine();
       while (lname.equals(""))
       {
           System.out.println("\tName error! Please enter your last name: ");
           lname = in.readLine();
       }


    System.out.print("\tPlease enter the loan Amount: $");  //asks for loan amount
    String input = in.readLine(); //user input

    double loanAmount = Double.parseDouble(input);

    while (loanAmount <=0){
            System.out.println("Invalid Number, please enter the loan Amount: ");
                         }


    System.out.print( //asks for interest rate
        "\tPlease enter the interest rate:  ");
    String inputa = in.readLine();
    double rateYears = Double.parseDouble(inputa);
 while (rateYears <=0){
            System.out.println("Invalid Number, please enter the interest rate: ");
 }

    System.out.print(
        "\tPlease enter the length of mortgage years ");
    String inputb = in.readLine();


    int termYears = Integer.parseInt(inputb);

    DecimalFormat precision2p = new DecimalFormat("$###,###.00");

    double rateMonthly = (rateYears / 12) / 100; // monthly calculation & interest rate
    double termMonths = (termYears * 12);        // monthly calculation & interest rate

    //monthlyPayment calculation continued
    double monthlyPayment = (loanAmount * rateMonthly) /
        (1 - Math.pow(1 + rateMonthly, -termMonths));


    System.out.println("\tThe mortgage payment will be: " +
                       precision2p.format(monthlyPayment));


      }
    }

Open in new window

Comment
Watch Question

Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Awarded 2011
Awarded 2011

Commented:
You should read indiviual values into String varaibale,
different name form the array, and then
assign this string to array element

Somethind like that:
String [] fname = new String[30];

String buff;

int count = 0;

buff  = in.reaLine();


fname[count] = buff;

count++;



Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
If this is an exercise in using arrays, you can hold and pass the numeric parameter in an array, e.g.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//Author 
import java.io.*;

import java.lang.String;

import java.text.*;

import java.util.Arrays;
import java.util.Scanner;


public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        String fname;
        String lname;
        double loanAmount;
        double rateYears;
        int termYears;

        System.out.println("\t\t\tWelcome to yet another Mortgage Calculator\n");

        {
            System.out.println("\tPlease enter your first name:");

            fname = in.readLine();

            while (fname.equals("")) {
                System.out.println(
                    "\tName error! Please enter your first name: ");
                fname = in.readLine();
            }
        }

        System.out.println("\tPlease enter your last name:");

        lname = in.readLine();

        while (lname.equals("")) {
            System.out.println("\tName error! Please enter your last name: ");
            lname = in.readLine();
        }

        System.out.print("\tPlease enter the loan Amount: $"); //asks for loan amount

        String input = in.readLine(); //user input

        loanAmount = Double.parseDouble(input);

        while (loanAmount <= 0) {
            System.out.println("Invalid Number, please enter the loan Amount: ");
        }

        System.out.print( //asks for interest rate
            "\tPlease enter the interest rate:  ");

        String inputa = in.readLine();
        rateYears = Double.parseDouble(inputa);

        while (rateYears <= 0) {
            System.out.println(
                "Invalid Number, please enter the interest rate: ");
        }

        System.out.print("\tPlease enter the length of mortgage years ");

        String inputb = in.readLine();

        termYears = Integer.parseInt(inputb);

	calculate(new double[] { loanAmount, rateYears, termYears });
    }

    static void calculate(double[] params) {

	double loanAmount = params[0];
	double rateYears = params[1];
	double termYears = params[2];
        DecimalFormat precision2p = new DecimalFormat("$###,###.00");

        double rateMonthly = (rateYears / 12) / 100; // monthly calculation & interest rate
        double termMonths = (termYears * 12); // monthly calculation & interest rate

        //monthlyPayment calculation continued
        double monthlyPayment = (loanAmount * rateMonthly) / (1 -
            Math.pow(1 + rateMonthly, -termMonths));

        System.out.println("\tThe mortgage payment will be: " +
            precision2p.format(monthlyPayment));
    }
}

Open in new window

Author

Commented:
Thank you all soooooo much!! The only issue I have now is the loop statement for the end.

This gives me the error of "Empty statement after while".
System.out.print("Would you want to enter another person? (y/n): ");
             while (in.readLine().equalsIgnoreCase("Y"));

Open in new window

CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you!!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.