Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class linearSearch
- {
- public static void main(String args[]) {
- Scanner reader = new Scanner(System.in);
- String search;
- String [] names = {"Mike","Jiho","Jin","Naomi"};
- String [] phonenumber = {"01012341234","0109991234","0100000234","01012129992224"};
- boolean found=false;
- int x=0;
- //linear search
- String again = "Y";
- while(again.equals("Y")){
- System.out.println("Enter name:");
- search = reader.next();
- while (found!=true && x<names.length) {
- if(search.equals(names[x])){
- System.out.println(search+" found at position "+x);
- System.out.println(phonenumber[x]);
- found=true;
- }
- x+=1;
- }
- if (found==false){
- System.out.println("not found");
- }
- System.out.println("Search again? Y or N");
- again = reader.next();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment