[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java array mortgage calculator user input and variable define issue

Posted on 2011-04-20
8
Medium Priority
?
536 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

0
Comment
Question by:marchbaby
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 1200 total points
ID: 35437898
if fname is array,
you cannot use i  as a string
String [] fname;


String fname  = in.readLine();

The same about other variable
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 35437905
Give your arrays different names and initialize them

eg.    String [] fnames = new String[3];    // can store 3 nmes

then store input into array using:

fnames[0] = fname;

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35437909
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++;



0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1200 total points
ID: 35437942

Something like that, as I showed below
But in fact you don't need in this case arrays,
as you are reading information about only one
mortgage

If you want to enater many sets of data, then you sohould
add some loop to this input and after calculating mortgage
should go back and ask again the same questions and then
accumulate the info in the arrays.
You also need some condition when to stop cllecting data -
say data for certain number of  loans, or until user enters empty line

For just one case you don't need arrays


I just showed how to assign the frrst elements of first and last name arrays
below:



/*
 * 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 Main1 {
  public static void main(String[] args) throws IOException {


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

   String [] fname = new String[30];
   String [] lname = new String[30];
   double [] loanAmount = new double[30];
   double [] rateYears = new double[30];
   int [] termYears = new int[30];

      int count =0;

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

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

    double loanAmount1 = Double.parseDouble(input);

    while (loanAmount1 <=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 rateYears1 = Double.parseDouble(inputa);
 while (rateYears1 <=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 termYears1 = Integer.parseInt(inputb);

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

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

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


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

      }
    }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35439380
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

0
 

Author Comment

by:marchbaby
ID: 35440829
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

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 35440894
You'd need  something like
String answer = null;
System.out.print("Would you want to enter another person? (y/n): ");
while ((answer = in.readLine()).equalsIgnoreCase("Y")) {
			// process next one
}

Open in new window

0
 

Author Closing Comment

by:marchbaby
ID: 35441190
Thank you!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question