Kodie
Kodie

Reputation: 1

Using an IF statement to control the return statement?

public static int seqSearch(int numRecords, String[] stuName,
      double[] stuGpa, String nameKey, double gpaKey)

        for(int i = 0; i < numRecords; i++)
           if(stuName[i] == nameKey && stuGpa[i] == gpaKey)
              return i;
        return -1;

So, how would I used an if statement to control this? I'm doing sequential search to find if the name is found in the array and if the gpa is in the array, then it should return the position it was found in (i). But, all it does do is return -1 and print out that none were found.

Upvotes: 0

Views: 267

Answers (9)

hooknc
hooknc

Reputation: 4991

To actually answer the question about the control of the if statement...

I believe what you're doing is fine with the the multiple return statements, BUT...

I personally prefer one entry point and only one exit point for my methods. It always feels dirty to me having multiple exit points.

So, I would consider the following code instead:

public static int seqSearch(int numRecords, String[] stuName, double[] stuGpa, String nameKey, double gpaKey)

    int value = -1;

    for(int i = 0; i < numRecords; i++) {  // Don't forget your braces, they aren't required, but wait until you add a newline and forget to add them...
       if(some.boolean().equals(comparison.here())) {
          value = i;
          break;
       }
    }
    return value;
}

Best of Luck.

Upvotes: 0

LBushkin
LBushkin

Reputation: 131676

You have two separate problems here:

  1. You should be comparing strings using the equals() method (or one of it's kin) - otherwise you are comparing whether two strings are the same reference (instance) rather than equivalent sequences of characters.
  2. You should avoid comparing doubles using == as equality for doubles is more nuanced. Check out this paper for more information about why.

See this question about why using == for floating point comparison is a bad idea in java.

Aside from that, I would also mention that your implementation makes the assumption that both stuName and stuGpa are arrays of the same length. This could easily not be the case ... and is probably something worth asserting before you begin iterating over the arrays.

Upvotes: 3

Kaushik Shankar
Kaushik Shankar

Reputation: 5619

Your problem is not the conditional if statement, but the conditional operator ==. == refers to the pointer value of the object where as the .equals method returns something computed by the object.

Like everyone has said before, switch your == to .equals in this next line:

public static int seqSearch(int numRecords, String[] stuName,
  double[] stuGpa, String nameKey, double gpaKey)

    for(int i = 0; i < numRecords; i++)
       if(stuName[i].equals(nameKey) && stuGpa[i] == gpaKey)
          return i;
    return -1;

Upvotes: 0

tjg184
tjg184

Reputation: 4676

The following is correct for the if statement. stuName[i] is a string so compare with .equals. stuGpa[i] is a double so use ==.

if(stuName[i].equals(nameKey_ && stuGpa[i] == gpaKey)

Upvotes: 0

Anther
Anther

Reputation: 1844

An essential problem is that

stuName[i] == nameKey

Is only comparing whether the objects are the same String Object in memory.

You actually want to use nameKey.equals(stuName[i]), to compare the actual string values. And you might want to use .equalsIgnoreCase for case insensitivity.

Upvotes: 0

kensen john
kensen john

Reputation: 5519

You are comparing two Strings. Strings are immutable. Please use "equalsIgnoreCase()" or "equals()" to compare Strings

See examples here http://www.java-samples.com/showtutorial.php?tutorialid=224

Upvotes: 0

Jim Blackler
Jim Blackler

Reputation: 23169

if(stuName[i] == nameKey is unlikely to be right, you are comparing object identities not string content. Try if(stuName[i].equals(nameKey)

Upvotes: 0

rich
rich

Reputation: 19404

You probably want

if (stuName[i].equals(nameKey) && stuGpa[i].equals(gpaKey))

Upvotes: 0

Robin Green
Robin Green

Reputation: 33033

Strings must be compared with .equals in Java, not ==.

if(stuName[i].equals (nameKey) && stuGpa[i] == gpaKey)

Upvotes: 1

Related Questions