Reputation: 79
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] + " ");
}
}
}
Upvotes: 0
Views: 45
Reputation: 34900
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
.
Upvotes: 0
Reputation: 2033
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
Upvotes: 1