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.

Recommended Answers

All 3 Replies

Looks like your professor has given you a fairly good start on what needs to happen inside that for-loop. Did you have a question that you needed help with?

If you tage your topic "Java" then it will be seen by many more Java experts who can help you.

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
    ..
    }
    based on the instructions I initailized String [] names = {""};
    private int index;
    index = 0;
boolean flag = true;
for( i = 0; i <names[index].length() ; 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
       index = index;
       name[index]= name;
       index = index + 1;

    }
    I am stuck on what do I need to write in the else block I tried writing the above, but I think I am doing something wrong or some part of the code is missing it's giving me NullpointerException
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.