Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package findname;
- import java.util.Scanner;
- public class FindName {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int numStudents;
- String[] students;
- String location;
- System.out.print("How many students? ");
- numStudents = input.nextInt();
- students = new String[numStudents];
- for (int i = 0; i < students.length; i++) {
- System.out.print("Name: ");
- students[i] = input.next();
- }
- System.out.print("What name would you like to find? ");
- String findName = input.next();
- location = Search.linear(students, findName) {
- if(location == -1) {
- System.out.println("That name was not found...");
- } else {
- System.out.print("First occurance is "+ location);
- }
- }
- } public static int linear(int[] students, int findNum) {
- int index = 0;
- while ((students[index] != findNum) && (index< students.length -1 )) {
- index += 1;
- }
- if (students[index] == findNum) {
- return(index);
- } else {
- return(-1);
- }
- }
- public static int linear(String[] students , String findName) {
- int index = 0;
- while ((students[index].compareTo(findName) !=0) && (index < array.length -1)) {
- index++;
- }
- if (students[index].equals(findName)) {
- return(index);
- } else {
- return(-1);
- }
- }
- }
Add Comment
Please, Sign In to add comment