public class StringSet {

    public static void main(String[] args){
        StringSet ss1 = new StringSet();
        ss1.insert("the");
        System.out.println(ss1);
    }
    /**
     * Creates an empty StringSet object.
     */

    private String[] data;

    public StringSet () {
        data = new String[0];

    }


    /**
     * If e is null, throws an IllegalArgumentException.
     * Else, if there is already an element in the StringSet that
     * is equal (in the .equals sense) to e, does nothing.
     * Otherwise, adds e to the StringSet. 
     */
    public void insert (String e) {
        int i = data.length;
        String[] newData = new String[data.length+1];

        // Copy data[j] to newData[j], for 0 <= j < i
        for (int j = 0; j < i; j++) {
            newData[j] = data[j];
        }

        // Add the element
        newData[i] = e;

        // Copy data[j] to newData[j+1], for i <= j < data.length
        for (int j = i; j < data.length; j++) {
            newData[j+1] = data[j];
        }

        // Save the new array in the object
        data = newData;
    }

Please provide full information of why it is null! You are expecting other people to assume what happens when you run the code...

Anyway, did you read the comment above the insert() method? You didn't follow the way it should. If the incoming argument is null, what would happen to your program? And what the loop in lines 31~33 is for? While you are copying from old to new array (and extend the array size), you should have checked whether or not the data already exists. If it exists, ignore line 44.

PS: Where is your toString() method? You are trying to display the class instance (using System.out.println()) but you didn't provide toString() to show how you display the class instance information.

in main method, you have printed object information.
insted you have to print something else,because you can't get what is information stored in class object.

so modify it.

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.