Your program will ask the user to input 10 letters eg e c z


Linear Search algorithm. Your program will ask the user to input 10 letters (e.g., e, c, z, x, k, m, o, a, d, f). The program will store these 10 letters into an array. Then the program will ask the user which letter the user wants to search for. If the letter is in the array, the program should return the position of the letter in the array. If the letter is NOT in the array, the message "Letter not found" should display. The user must press "E" to exit. and lastly its has to be a GUI.

import javax.swing.JOptionPane;

public class LetterSearchGUI {

 public static int search(String[] arr, String key) {

  int size = arr.length;

  for (int i = 0; i < size; i++) {

   if (key.equalsIgnoreCase(arr[i])) {

    return i;

   }

  }

  return -1;

 }

 public static void main(String a[]) {

  do {

   String[] input = new String[10];

   for (int i = 0; i < 10; i++) {

    input[i] = JOptionPane.showInputDialog("Enter the letter "

      + (i + 1) + ": ");

   }

   String searchKey = JOptionPane

     .showInputDialog("Enter the letter the user wants to search for: ");

   int pos = search(input, searchKey);

   if (pos == -1) {

    JOptionPane.showMessageDialog(null, "Letter not found");

   } else {

    JOptionPane.showMessageDialog(null, "Key " + searchKey

      + " found at index: " + pos);

   }

 

   // string for exit button

   String s = JOptionPane

     .showInputDialog("Press E to exit the program or anything to continue: ");

   if ("E".equalsIgnoreCase(s)) {

    break;

   }

  } while (true);

 }

}

Solution Preview :

Prepared by a verified Expert
Basic Computer Science: Your program will ask the user to input 10 letters eg e c z
Reference No:- TGS02597667

Now Priced at $10 (50% Discount)

Recommended (95%)

Rated (4.7/5)