KnowledgeBoat Logo

Computer Applications

Write a program in Java to store 20 different names and telephone numbers of your friends in two different Single Dimensional Arrays (SDA). Now arrange all the names in alphabetical order and display all the names along with their respective telephone numbers using selection sort technique.

Java

Java String Handling

43 Likes

Answer

import java.util.Scanner;

public class KboatTelephoneBook
{
    public static void main(String args[]) {
        final int SIZE = 20;
        Scanner in = new Scanner(System.in);
        String names[] = new String[SIZE];
        long telNos[] = new long[SIZE];
        System.out.println("Enter " + SIZE + " names and telephone numbers");
        for (int i = 0;  i < SIZE; i++) {
            System.out.print("Enter Name: ");
            names[i] = in.nextLine();
            System.out.print("Enter telephone number: ");
            telNos[i] = in.nextLong();
            in.nextLine();
        }
        
        //Selection Sort
        for (int i = 0; i < SIZE - 1; i++) {
            int min = i;
            for (int j = i + 1; j < SIZE; j++) {
                if (names[j].compareToIgnoreCase(names[min]) < 0) {
                    min = j;
                }
            }
            String temp = names[min];
            names[min] = names[i];
            names[i] = temp;
            
            long t = telNos[min];
            telNos[min] = telNos[i];
            telNos[i] = t;
        }
        
        System.out.println("Name\tTelephone Number");
        for (int i = 0; i < SIZE; i++) {
            System.out.println(names[i] + "\t" + telNos[i]);
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program in Java to store 20 different names and telephone numbers of your friends in two different Single Dimensional Arrays (SDA). Now arrange all the names in alphabetical order and display all the names along with their respective telephone numbers using selection sort technique.BlueJ output of Write a program in Java to store 20 different names and telephone numbers of your friends in two different Single Dimensional Arrays (SDA). Now arrange all the names in alphabetical order and display all the names along with their respective telephone numbers using selection sort technique.

Answered By

11 Likes


Related Questions