Humeey4
Humeey4

Reputation: 47

Checking if a string exists in array

I'm trying to check if a name has already been used in the array, but it's only working for the spot [0]. I'm assuming its from the for loop in boolean only going through once and not incrementing up to check the other spots?

Tried changing different if's and while loops

if (depotCount < 4){ // Runs if the depots are less than 4
    System.out.println("Enter your depots name");
    name = console.next().toLowerCase();
    if (check(depots, name) == true){
       System.out.println("Depot name already exists");
       break;
    }
    else {
      addDepot(depots, name);   
    }              
} else { 
     System.out.println("Only 4 depots are allowed");
     break;
}
public boolean check(Depot [] depots, String name){
  boolean check = false;
  for (int i = 0; i < depots.length; i++){
     if(depots[i].getDepotName().equals(name))
       return true;
     else {
       return false;
     }
   }
  return check;

}

So it is working if a first name as "Warehouse" and I try to put "Warehouse" a second time. but if I try to put the same name as the 2nd slots it doesn't come back as true.

Upvotes: 2

Views: 661

Answers (5)

Odilbek Utamuratov
Odilbek Utamuratov

Reputation: 341

In my opinion you should remove: else { return false; } from your code:

public boolean check(Depot[] depots, String name) {
    boolean check = false;
    for (int i = 0; i < depots.length; i++) {
        if (depots[i].getDepotName().equals(name))
            return true;
    }
    return check;
}

Upvotes: 1

Hien Nguyen
Hien Nguyen

Reputation: 18975

You need remove return false; in for loop, if you put there, for loop run only one time with index 0.

public boolean check(Depot[] depots, String name) {
    boolean check = false;
    for (int i = 0; i < depots.length; i++) {
        if (depots[i].getDepotName().equals(name))
            return true;
    }
    return check;
}

You can only short like this without check variable.

public boolean check(Depot[] depots, String name) {
    for (int i = 0; i < depots.length; i++) {
        if (depots[i].getDepotName().equals(name))
            return true;
    }
    return false;
}

Upvotes: 8

GMc
GMc

Reputation: 1774

The problem is that you always return on the first iteration of the loop.

Try modifying your code as follows:

public boolean check(Depot[] depots, String name) {
    for (int i = 0; i < depots.length; i++) {
        if (depots[i].getDepotName().equals(name))
             return true;
    }
    return false;
}

Also, you do not need to compare for true in an if statement. That is you can change this:

if (check(depots, name) == true) {

to this:

if (check(depots, name)) {

Also, you might want to check out java's HashMap. These have methods such as:

  • containsKey(key) checks to see if the key (depotName) is present or not
  • get (key) retrieves the record by Key
  • put (key, value) allows you to add (put) values into the map.
  • there is no limit (e.g. you don't need to pre-declare the size like you do with an Array).
  • They are quite fast - especially as the number of entries get's larger.

As for the value, that can be anything you like. For example it might be a String containing the companies address. It could be an integer containing the number of employees at that company. Or, and this is the best one, it could be an instance of a class containing every conceivable detail about the company!

Alternatively if you don't need to store a value, you can always use a KeySet, but HashMaps are probably more useful for you.

Upvotes: 2

TheSmile
TheSmile

Reputation: 74

It's because the you have return false;. This simply ends the method execution (and by doing so the for loop) because the value false have been returned to the method call.

public boolean check(Depot[] depots, String name) {
    boolean check = false;
    for (int i = 0; i < depots.length; i++) {
        if (depots[i].getDepotName().equals(name))
            return true;
    }
    return check;
}

Upvotes: 1

Alan
Alan

Reputation: 976

Inside your for loop you have an "else return false". what this does is, if you find something that is not equal you immidiatly return false. However, if you return on any occasion in your method, the method is over thus it doesnt loop through all depots

public boolean check(Depot [] depots, String name){
    for (int i = 0; i < depots.length; i++){
        if(depots[i].getDepotName().equals(name))
            return true;
    }
    return false;
}

Upvotes: 2

Related Questions