2

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.

5
  • debugging is the key Commented May 8, 2019 at 8:11
  • 4
    You never need to write something == true: it's equivalent to use just something. Commented May 8, 2019 at 8:13
  • @AndyTurner till recent I also thought so... but maybe it is easier to read (more fluent ((for someone not that used to writing code)) ) Commented May 8, 2019 at 8:24
  • @CarlosHeuberger one of the important things about learning to write code is learning to write idiomatic code, which necessarily means learning to read idiomatic code. Commented May 8, 2019 at 8:29
  • agreed but not everyone reading code must be writing code (e.g. testers, managers, other language, ...) Commented May 8, 2019 at 9:41

5 Answers 5

8

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;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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;
}

Comments

2

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.

Comments

1

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;
}

Comments

1

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;
}

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.