* Read in a list of names from the user using a sentinel value of -1 to mark the end of the list.
* Only add the name to the array if the name is not already on the list. (EX: If "Mary" is on the user list 3 times, it should only be in the Names array one time.)
* Print out the total number of names, and print out all of the names in the array.
* Remember to use methods whenever you can.
Here are my confusions and errors:
* I cannot find the exception because it seems as if I am not using an variable that was not set to an object.
* I need a way to have a count for when the names are not repeated, and a different count for when they are repeated.
import java.util.Scanner;
public class NameList
{
public static void main(String [] args)
{
Scanner reader = new Scanner(System.in);
// An array of 20 String Objects is created
String [] names = new String [20];
System.out.println("Welcome to the Name List Program:-)");
System.out.println("***********************************");
// Instance variables
String appellation;
int count = 0;
final String SENTINEL = "-1";
boolean isFound;
// Ask the user for a first name or "-1" if the user would not like to proceed with the program
System.out.print("\nEnter a first name (" + SENTINEL + " to quit): ");
appellation = reader.nextLine();
// Keeps asking the user for first names until "-1" is entered
while (!appellation.equals(SENTINEL))
{
count++;
System.out.print("Enter a first name (" + SENTINEL + " to quit): ");
appellation = reader.nextLine();
}
isFound = searchForName(appellation, count, names);
if (isFound == false)
{
System.out.println("\nThere are a total of " + count + " names.");
for (int index = 0; index < count; index++)
{
names[index] = appellation;
}
System.out.println("The names in the array are:");
for (int index = 0; index < count; index++)
{
System.out.println(names[index]);
}
}
}
public static boolean searchForName(String appellation, int logicalSize, String [] nameArray)
{
boolean answer = false;
for (int i = 0; i < logicalSize; i++)
{
if (nameArray[i].equals(appellation))
{
//logicalSize = logicalSize - 1;
answer = true;
}
else
{
answer = false;
}
}
return answer;
}
}

New Topic/Question
Reply



MultiQuote





|