Link to home
Start Free TrialLog in
Avatar of marchbaby
marchbaby

asked on

Java not saving string into array

Sorry to keep reposting my code, just different issues I keep running into. My input is not being saved into the array and I do not know what I am doing incorrectly.


import java.io.*;
import java.text.*;
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 = new String[30];
        String[] lname = new String[30];
        String[] phoneNum = new String [30];
        double[] loanAmount = new double[30];
        double[] rateYears = new double[30];
        int[] termYears = new int[30];
        boolean yes = true;

        int count = 0;

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

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

            fname [count] = in.readLine();

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

            //fname[count] = fname1; //into the array it goes


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

            lname [count] = in.readLine();

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

            //lname[count] = lname1; //into the array

    //sort array



            //public phoneNum extends Main{
             //   String phoneNum;
           // }

           System.out.println("Please enter your phone number with dashes"); //asking for phone number
            phoneNum [count] = in.readLine(); //getting user input



            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: ");
            }

            loanAmount[count] = loanAmount1; //into the array it goes

            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: ");
            }

            rateYears[count] = rateYears1;

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

            String inputb = in.readLine();

            int termYears1 = Integer.parseInt(inputb);

            termYears[count] = termYears1; //into the array it goes

            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(lname[j]);
            
            
            System.out.println("\tClient last name: \n" +lname);
            System.out.println("\tClient first name: \n" +fname);
            System.out.println("\tClient phone Number: \n" +phoneNum);

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

            {
                System.out.print(
                    "Would you want to enter another person? (y/n): ");

                String inputc = in.readLine();
                yes = inputc.equalsIgnoreCase("y");
            }
        }
         
    }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are you incrementing count value anyway - don't have search now, but I don't see where you oincrement count value from one input to another
Avatar of marchbaby
marchbaby

ASKER

How do I do that?
I posted the code yesterday which works.
Why would not you use it?
Let me know if you have questions about it.
count++;
Yan,

The other code still didn't save into the array, which is what made me realise that nothing was saving into the array.
You need to increment counter say befor e you ask for do yoiu want to enter one more account
Did you run the code i posted?
I still get a display of Client last name:

[Ljava.lang.String;@a90653
        Client first name:
[Ljava.lang.String;@de6ced
        Client phone Number:
[Ljava.lang.String;@c17164

I don't know what I'm doing incorrectly.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CEHJ  - yes, the display is above ^

Thank you for helping!
Yes this pohone lshould be changhed because uyou don't need to have this class - look below at lname - do for all other fields the same as for lname.
It wirked for me - it should work for you - I just left a little work for youi so you couild better understand.
>>I still get a display of Client last name:

That's not the code i posted
No - it's the display/output I get when it runs.
This is the sort of thing you should get from what i posted (fname array)
Would you want to enter another person? (y/n): n
[A, B, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]

Open in new window

Go back exactly to the code I posted yestersday - don't use this one which yoiu posted today - it has many problems - I fixed them all yuesterdsay
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Loook at the post which shows you the output togethjer with the code
I understand now! Thank you!! My teacher is requiring us to write this code that is not the way this type of program would really be written. Interestly enough, this isn't even covered in our textbook.
Thank you!!
>> My teacher is requiring us to write this code that is not the way this type of program would really be written.

Yes, i'm afraid it's perfectly normal in CS classes to teach you to write code that would either never need to be written or should never be written in real life. The things that are really difficult to do, such as to design and build solid software that actually works and doesn't bust the budgets of corporations they (often) don't consider important enough to bother teaching ;)