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 not saving string into array

marchbaby
marchbaby asked
on
Medium Priority
595 Views
Last Modified: 2012-05-11
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

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Awarded 2011
Awarded 2011

Commented:
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

Author

Commented:
How do I do that?
Awarded 2011
Awarded 2011

Commented:
I posted the code yesterday which works.
Why would not you use it?
Let me know if you have questions about it.
Awarded 2011
Awarded 2011

Commented:
count++;

Author

Commented:
Yan,

The other code still didn't save into the array, which is what made me realise that nothing was saving into the array.
Awarded 2011
Awarded 2011

Commented:
You need to increment counter say befor e you ask for do yoiu want to enter one more account
CERTIFIED EXPERT
Top Expert 2016

Commented:
Did you run the code i posted?

Author

Commented:
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.
Awarded 2011
Awarded 2011
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
CEHJ  - yes, the display is above ^

Thank you for helping!
Awarded 2011
Awarded 2011

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>I still get a display of Client last name:

That's not the code i posted

Author

Commented:
No - it's the display/output I get when it runs.
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Awarded 2011
Awarded 2011

Commented:
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
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Awarded 2011
Awarded 2011

Commented:
Loook at the post which shows you the output togethjer with the code

Author

Commented:
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.

Author

Commented:
Thank you!!
CERTIFIED EXPERT
Top Expert 2016

Commented:
>> 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 ;)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*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.