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

x
?
Solved

Java not saving string into array

Posted on 2011-04-22
21
Medium Priority
?
560 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

0
Comment
Question by:marchbaby
  • 8
  • 7
  • 6
21 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1336 total points
ID: 35448065
It will if you print the array(s) and you increment count properly. See below
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");
            }
	    count++;
        }
	System.out.println(Arrays.toString(fname));
    }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35448068
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
0
 

Author Comment

by:marchbaby
ID: 35448077
How do I do that?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 47

Expert Comment

by:for_yan
ID: 35448078
I posted the code yesterday which works.
Why would not you use it?
Let me know if you have questions about it.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35448083
count++;
0
 

Author Comment

by:marchbaby
ID: 35448087
Yan,

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

Expert Comment

by:for_yan
ID: 35448089
You need to increment counter say befor e you ask for do yoiu want to enter one more account
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35448095
Did you run the code i posted?
0
 

Author Comment

by:marchbaby
ID: 35448098
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.
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 664 total points
ID: 35448106
It did - return to that code - it sasves and then sorts the array and then prints - see the output it prints two last names there -just look at other fields other than lname and do everything analogously - that code worked as I execuited it and it of course incremenets the count value
0
 

Author Comment

by:marchbaby
ID: 35448110
CEHJ  - yes, the display is above ^

Thank you for helping!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35448128
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35448134
>>I still get a display of Client last name:

That's not the code i posted
0
 

Author Comment

by:marchbaby
ID: 35448142
No - it's the display/output I get when it runs.
0
 
LVL 86

Expert Comment

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

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35448156
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
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1336 total points
ID: 35448166
But as i mentioned yesterday, this isn't going anywhere as it happens - it's not going to be possible (practicably) to print a lot of parallel arrays properly. You need ONE array (or a collection) of something like a Customer class
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35448174
Loook at the post which shows you the output togethjer with the code
0
 

Author Comment

by:marchbaby
ID: 35448184
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.
0
 

Author Closing Comment

by:marchbaby
ID: 35448200
Thank you!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35448257
>> 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 ;)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

865 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