/* SearchNameFixed.java written by Craig Persiko
CS 111A Solution to in-class exercise
Uses a hard-coded array of names,
inputs one name from user, and searches
to see if it's in the list.
*/
import java.util.Scanner;
class SearchNameFixed
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String[] names = {"Craig", "Bassam", "Jennifer", "Melissa", "Anna"};
String inputtedName;
boolean nameFound = false;
System.out.println("Please enter your name:");
inputtedName = scan.nextLine();
for(int i=0; i<names.length && !nameFound; i++)
{
if(names[i].equalsIgnoreCase(inputtedName))
{
System.out.println("You are number " + i + " in the group");
nameFound = true;
}
}
if(!nameFound)
System.out.println("You are not in the group.");
}
}
/* Sample Output:
[cpersiko@fog cs111a]$ java SearchNameFixed
Please enter your name:
Craig
You are number 0 in the group
[cpersiko@fog cs111a]$ java SearchNameFixed
Please enter your name:
Bob
You are not in the group.
[cpersiko@fog cs111a]$ java SearchNameFixed
Please enter your name:
Jennifer
You are number 2 in the group
[cpersiko@fog cs111a]$
*/
syntax highlighted by Code2HTML, v. 0.9