In Java The program should prompt the user to input a group

In Java:

The program should prompt the user to input a group of characters (a string). The chars will refer to people’s names and will be entered as one word names for each group waiting. There will be exactly 4 names that need to be entered into the list. The longest name will be less than 30 characters long.

Sort the names into alphabetical order.

After all of the names have been entered, the program should prompt the user to input a name. After the name has been entered, the program should conduct a search of the list looking for the name. The search needs to be repeated till the user inputs “over”. The search can be a linear search (the list is very short) or a binary search.

Solution

// SortSearch.java
import java.util.*;

public class SortSearch
{
public static void mergeSort(String[] names)
{
   if (names.length >= 2)
   {
       String[] l = new String[names.length / 2];
       String[] r = new String[names.length - names.length / 2];

   for (int i = 0; i < l.length; i++)
   {
       l[i] = names[i];
   }

   for (int i = 0; i < r.length; i++)
   {
       r[i] = names[i + names.length / 2];
   }

   mergeSort(l);
   mergeSort(r);
   merge(names, l, r);
}
}

public static void merge(String[] names, String[] l, String[] r)
{
   int index1 = 0;
   int index2 = 0;
   for (int i = 0; i < names.length; i++)
   {
       if (index2 >= r.length || (index1 < l.length && l[index1].compareToIgnoreCase(r[index2]) < 0))
       {
           names[i] = l[index1];
           index1++;
       }
       else
       {
           names[i] = r[index2];
           index2++;
       }
   }
}

   public static void main(String[] args)
   {
   Scanner scanner = new Scanner( System.in );

       //Create array of Strings with AT LEAST 5 names
       String[] names = new String[4];

       for (int i = 0; i < 4; i++ )
       {
           System.out.print( \"Enter name: \" );
       String input = scanner.nextLine();
       names[i] = input;
       }
      
       System.out.println(\"\ Unsorted list:\");
       for (String name : names)
       {
           System.out.println(name);
       }

       // sorting the list of names
       mergeSort(names);

       System.out.println(\"\ Sorted list:\");
       for (String name : names)
       {
           System.out.println(name);
       }

       System.out.println(\"\ \");
       while(true)
       {
           System.out.println(\"Input a name to search: \");
           String input = scanner.nextLine();
           int flag = 1;

           if (input.equals(\"over\"))
           {
               System.out.println(\"Good Bye!\ \");
               break;  
           }
           for (String name : names)
           {
               if(name.equals(input))
               {
                   System.out.println(\"Name present in list\ \");
                   flag = 0;
               }
           }

           if(flag == 1)
               System.out.println(\"Name not present in list\ \");

       }
   }

}

/*
output:

Enter name: jason
Enter name: roy
Enter name: alex
Enter name: hales

Unsorted list:
jason
roy
alex
hales

Sorted list:
alex
hales
jason
roy


Input a name to search:
roy
Name present in list

Input a name to search:
ayush
Name not present in list

Input a name to search:
hales
Name present in list

Input a name to search:
over
Good Bye!

*/

In Java: The program should prompt the user to input a group of characters (a string). The chars will refer to people’s names and will be entered as one word na
In Java: The program should prompt the user to input a group of characters (a string). The chars will refer to people’s names and will be entered as one word na
In Java: The program should prompt the user to input a group of characters (a string). The chars will refer to people’s names and will be entered as one word na

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site