public class NaveenSingleton {
private String name;
private String [] names;
private static NaveenSingleton uniqueInstance;
private NaveenSingleton() {
}
public static NaveenSingleton getInstance() {
if(uniqueInstance == null)
{
uniqueInstance = new NaveenSingleton();
}
return uniqueInstance;
}
public void setName(String n)
{
name = n;
//Search for duplicate name here, before setting names
// Do a for loop of the names array using the String method equals() or equalsIgnoreCase
//Only save the name if it does not appear in this private array
}
public String getName()
{
return name;
}
}
import java.util.*;
public class NaveenRunTrack {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
NaveenSingleton singleton = NaveenSingleton.getInstance();
Scanner keyboard = new Scanner(System.in)
String runnerName [] = new String[8];
for(int i =0; i<8; i++)
{
System.out.print("Enter the first and last name runner in track no " + i + ":");
runnerName[i] = keyboard.nextLine();
singleton.setName(runnerName[i]);
runnerName[i] = singleton.getName();
}
for(int i =0; i<8; i++)
{
System.out.println("Name of the runner in track of " + i + ": " + runnerName[i]);
}
}
in the setName method in NaveenSingleton my professsor what me to write a .equals statements to check if there are duplicates in the array of name the professor gave me hints but not sure how to complete it
here is the hint
Naveen,
In a for loop, compare each item in the names array against the name you just received. I would first initialize this array to the empty string (2 quotation marks, ""). Also, I would have a private variable named index, for instance to keep track of the last array index you filled.
For example:
index = 0;
flag = true;
for( i = 0; i < ; i++){
//This means a duplicate name
flag = false;
break;
} If (flag == true) {
//save the name
names[index] = name;
index = index + 1;
} else {
//Ask the user for a new name
..
}
This is just an outline, I would expect to lay it out.