JackHoughton00

findName

May 18th, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. package findname;
  2. import java.util.Scanner;
  3.  
  4. public class FindName {
  5.  
  6. public static void main(String[] args) {
  7. Scanner input = new Scanner(System.in);
  8. int numStudents;
  9. String[] students;
  10. String location;
  11.  
  12. System.out.print("How many students? ");
  13. numStudents = input.nextInt();
  14. students = new String[numStudents];
  15. for (int i = 0; i < students.length; i++) {
  16. System.out.print("Name: ");
  17. students[i] = input.next();
  18. }
  19. System.out.print("What name would you like to find? ");
  20. String findName = input.next();
  21.  
  22. location = Search.linear(students, findName) {
  23. if(location == -1) {
  24. System.out.println("That name was not found...");
  25.  
  26. } else {
  27. System.out.print("First occurance is "+ location);
  28. }
  29.  
  30. }
  31.  
  32. } public static int linear(int[] students, int findNum) {
  33. int index = 0;
  34. while ((students[index] != findNum) && (index< students.length -1 )) {
  35. index += 1;
  36. }
  37. if (students[index] == findNum) {
  38. return(index);
  39.  
  40. } else {
  41. return(-1);
  42. }
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. public static int linear(String[] students , String findName) {
  51. int index = 0;
  52. while ((students[index].compareTo(findName) !=0) && (index < array.length -1)) {
  53. index++;
  54. }
  55. if (students[index].equals(findName)) {
  56. return(index);
  57. } else {
  58. return(-1);
  59. }
  60. }
  61.  
  62. }
Add Comment
Please, Sign In to add comment