0
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.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
Kodie
  • 1
  • 2
    Use equals not == for String comparison – Erik Apr 12 '11 at 20:20
  • 1
    Comparing doubles with == can be rough too. – corsiKa Apr 12 '11 at 20:22
  • welcome to Stackoverflow, the only site where people are so eager to answer your question you get 5 answers in the first 3 minutes. :D :) – Gordon Gustafson Apr 12 '11 at 20:24
  • I hope this isn't homework. If your professor is teaching you to use static methods passing in string arrays for searching instead of having you do it in an Object Oriented manner, he's doing you a disservice. – corsiKa Apr 12 '11 at 20:24
  • @Crazy maybe on the easy ones. When you ask some of the more difficult questions, you're not always so fortunate (which is good. More answers != better answers.) – corsiKa Apr 12 '11 at 20:25
  • shouldn't there be a '{' after 'gpaKey)' and a '}' before '-1;' ? – Bastardo Apr 12 '11 at 20:27
  • Kodie, I'm curious. Why are you looking for both a matching name and a matching GPA in the list? Can the same name show up more than once? – jprete Apr 12 '11 at 20:37
  • @Emre yes. @glowcoder I disagree. OOP should be introduced at a later stage, if at all (CMU have dropped it from the 1st year curriculum entirely, in favour of functional programming, and made it optional in the 2nd year. OK maybe that's a bit too forward-thinking/idealistic. But it's also probably kinder on the less geeky students.) – Robin Green Apr 12 '11 at 20:41

9 Answers9

3

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.

Community
  • 1
  • 1
LBushkin
  • 129,300
  • 32
  • 216
  • 265
1

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

if(stuName[i].equals (nameKey) && stuGpa[i] == gpaKey)
Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • 3
    "must" is a bit strong of a statement. And, there are legitimate times when you want to use `==`, for example when working with `public static final String` it may be useful to know if it came from your constant source, and not from an imposter simulating it. – corsiKa Apr 12 '11 at 20:23
0

You probably want

if (stuName[i].equals(nameKey) && stuGpa[i].equals(gpaKey))
rich
  • 18,987
  • 11
  • 75
  • 101
  • It turned out to be {if (stuName[i].equals(nameKey) && stuGpa[i] == gpaKey)} Because stuGpa is a double array; which I neglected to mention. I apologize, but thanks for the help! – Kodie Apr 12 '11 at 20:24
0

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

Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
0

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

kensen john
  • 5,439
  • 5
  • 28
  • 36
0

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.

Anther
  • 1,834
  • 12
  • 13
0

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)
tjg184
  • 4,508
  • 1
  • 27
  • 54
0

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;
Kaushik Shankar
  • 5,491
  • 4
  • 30
  • 36
0

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.

hooknc
  • 4,854
  • 5
  • 31
  • 60