Link to home
Start Free TrialLog in
Avatar of marchbaby
marchbaby

asked on

Java sort array alphabetically and print list

Hi,
First I am extremely grateful for the help I have received so far here. I have to sort and display the mortgage information with the sorting done alphabetically by last name. I know the phone number is written in a subclass, but I had to do that for the assignment. I have some stuff written, but I'm at a loss how to print all the array info starting with last name, then first name alphabetically.
import java.io.*;
import java.text.*;


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

            String fname1 = in.readLine();

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

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

            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; //into the array

    //sort array

            java.util.Arrays.sort(lname, String.CASE_INSENSITIVE_ORDER);


            class phoneNum extends Main{
                String phoneNum;
            }

           System.out.println("Please enter your phone number with dashes"); //asking for phone number
            String phoneNum = 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("\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

Avatar of for_yan
for_yan
Flag of United States of America image


Thiis is how you can sort arry:

ArrayList al = new ArrayList();

for(int j=0; j<fnames.length; j++)al.sdd(fnames[j]);

Collections.sort(al);

for(int j=0; j<al.size(); j++)fnames[j] = (String) al.get(j);
















Spelling correction:

ArrayList al = new ArrayList();

for(int j=0; j<fnames.length; j++)al.add(fnames[j]);

Collections.sort(al);

for(int j=0; j<al.size(); j++)fnames[j] = (String) al.get(j);



You can do it with all your arrays or you can make a method

public String [] getOrderedArray(String [] fnames){
ArrayList al = new ArrayList();

String fnames1 = new String[fnames.length];

for(int j=0; j<fnames.length; j++)al.add(fnames[j]);

Collections.sort(al);

for(int j=0; j<al.size(); j++)fnames1[j] = (String) al.get(j);

return fnames1;

}

after that you can write everywere in your code:

lnames = getOrderedArray(lnames);
fnames = getOrderedArray(fnames);
and arrays lnames, fnames become ordered



You can usee Array.sort,
but it should be moved to another location in you program (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];
        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:");

            String fname1 = in.readLine();

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

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

            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; //into the array

    //sort array

            //Arrays.sort(lname);

           for(int j=0; j<lname.length; j++) System.out.println(lname[j]);


            class phoneNum extends Main{
                String phoneNum;
            }

           System.out.println("Please enter your phone number with dashes"); //asking for phone number
            String phoneNum = 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("\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");
            }
        }
         Arrays.sort(lname);
           for(int j=0; j<lname.length; j++) System.out.println(lname[j]);
    }
    }

Open in new window

Avatar of CEHJ
You will eventually run into trouble with your arrays - parallel arrays are difficult to maintain and won't scale well.

What you be much better is to have a Customer class containing the correct data, and then you can sort instances of Customer
No actually the code which I posted how to order was correct.
You can also use Array.sort - also in general correct
Your code is incorrect and i diidn't correct it either,
requires some more corrections

In general parallel arrays are not good but at this stage you probably would
be OK with them - to sort instnaces of Customer will be next stage
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America 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
Avatar of marchbaby
marchbaby

ASKER

I understand now!! Thank you!