0

So I have an array which has x indexes according to what the user inputs (NoOfArrays). A method called add is used to add a string to the next null index in the array.. but none of this works..

so when an array of x indexes are created, i should be able to add a string into the array, remove it, check if the array is full, check if a "user input" string is already in the array, etc

public class Bag1
{
    private String[] store;

    public Bag1(int NoOfArrays)
    {
        store = new String[NoOfArrays];
        for (int i=0;i>store.length;i++){
            store[i] = null;
        }
    }
    public boolean isFull(){
        boolean isTrue = true;
        for (int i=0;i>store.length;i++) {
            if (store[i] == null){
                isTrue = false;
                break;
            }
        }
        return isTrue;
    }

    public void add(String s){
        for (int i=0;i>store.length;i++){
            if (store[i] == null){
                store[i] = s;
                break;
            }
        }
    }

    public boolean contains(String s){
        boolean isTRUE = false;
        for (int i=0;i>store.length;i++){
            if (store[i] == s){
                isTRUE = true;
            }
            else{
                System.out.println("Does not have " + s);
            }
        }        
        return isTRUE;
    }

    public void remove(String s){
        for (int i=0;i>store.length;i++)
            if (store[i] ==s){
                store[i] = null;
                break;
            }
    }

    public void showStrings(){
        for (int i=0;i>store.length;i++){
            System.out.print(store[i] + " ");
        }
    }
}
1

2 Answers 2

1

Your for loop is incorrect

for (int i=0;i>store.length;i++){

Since i=0, the condition i>store.length will evaluate to false, and loop will not be entered

Change it to

 for (int i=0;i<store.length;i++){

Also, as @Pshemo commented, use equals() to compare string

Sign up to request clarification or add additional context in comments.

Comments

0

There are several critical problems in your code. Main of them is that you use invalid method of String's compare: store[i] ==s. It should be s.equals(store[i]).

Another one is incorrect loops condition structure. As mentioned @sam you should change it from i>store.length to i<store.length.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.