22. Program to search an element in an array. Ask-before-iterating loop sets how many elements to be inserted. Afterwards, use the loop to store values/data from user. When the loop ends, ask the user to search for an element. If an element is found, display it is found and where position it is being located. Otherwise, display element is not found.
import javax.swing.JOptionPane;
public class LabExer22 {
public static void main(String[] args) {
int limit=Integer.parseInt(JOptionPane.showInputDialog("Enter how many elements to be inserted: "));
String elements[]=new String [limit];
String search="";
int base=0;
int index=0;
String found="Element is found at index ";
String notFound="Element is not found";
for(int a=0;a<limit;a++){
elements[a]=JOptionPane.showInputDialog("Enter data: ");
}
search=JOptionPane.showInputDialog("Thank You for entering data!\n"+"Please enter an element to search: ");
search=search.toLowerCase();
for(int x=0;x<limit;x++){
if(elements[x].toLowerCase().equals(search)){
base++;
index=x;
}
}
if(base>0){
JOptionPane.showMessageDialog(null, found +index,"AMANTE's Program",JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null, notFound,"AMANTE's Program",JOptionPane.INFORMATION_MESSAGE);
}
JOptionPane.showMessageDialog(null,"Thank You! I hope to serve you next time..","AMANTE's Program",JOptionPane.PLAIN_MESSAGE);
}
}
Walang komento:
Mag-post ng isang Komento