Advertisement
mrScarlett

year 10 linear solution

Feb 2nd, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import java.util.*;
  2. public class linearSearch
  3. {
  4. public static void main(String args[]) {
  5. Scanner reader = new Scanner(System.in);
  6. String search;
  7. String [] names = {"Mike","Jiho","Jin","Naomi"};
  8. String [] phonenumber = {"01012341234","0109991234","0100000234","01012129992224"};
  9. boolean found=false;
  10. int x=0;
  11. //linear search
  12. String again = "Y";
  13. while(again.equals("Y")){
  14. System.out.println("Enter name:");
  15. search = reader.next();
  16. while (found!=true && x<names.length) {
  17. if(search.equals(names[x])){
  18. System.out.println(search+" found at position "+x);
  19. System.out.println(phonenumber[x]);
  20. found=true;
  21. }
  22. x+=1;
  23.  
  24. }
  25. if (found==false){
  26. System.out.println("not found");
  27. }
  28. System.out.println("Search again? Y or N");
  29. again = reader.next();
  30. }
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement