views:

41

answers:

3

When I run the program and enter a rank & gender it yells at me and tells me it is an invalid gender. I cannot see why if I enter "male" into the console, it does not equal the string "male"? Can please explain to me why this doesn't work and perhaps some suggestions on how to fix it? Thanks!

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class BabyNames {

public static void main(String[] args) throws IOException {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter male or female: ");
    String gender1 = input.next();
    System.out.print("Enter rank: ");
    int rank = input.nextInt();         
    while (!(gender1 == "female" || gender1 == "male")){
            System.out.println("Please try again with a valid gender.");
            System.out.println("");
            System.out.println("Enter male or female: ");
            gender1 = input.next();
        }
    while (rank <= 0 || rank > 1000){
        System.out.println("Please try again with a valid rank.");
        System.out.println("");
        System.out.println("Enter rank: ");
        rank = input.nextInt();
    }

    findRank(gender1, rank);
}

public static void findRank(String gender2, int rank) throws IOException {
    File babyNames = new File("/home/skatty14/Downloads/BabyNames.txt");
    Scanner input = new Scanner(babyNames);
    int rankCount = 0;
    while (input.hasNextLine()){
        rankCount++;
        System.out.println(rankCount);
        if (gender2 == "male"){
            String name = input.next();
            System.out.println(name);
        }
        if (gender2 == "female"){
            String name = input.next();
            System.out.println(name);
        }
      }
   }
}
+4  A: 

To compare Strings, you need to use .equals() so change all of your String comparisons with == to use .equals() instead.

if(gender1 == "male") { ... }

would become,

if(gender1.equals("male")) { ... }

So on and so forth.

Anthony Forloney
Just to be that much more 'efficient' should you change the second `if` to `else if`?
Glenn Nelson
+1  A: 

The '==' operator tests for equivalence of references to Strings, not contents of Strings, hence why your code is not giving you the expected results.

Try using the form

if (gender1.equals("female"))

etc. instead.

cgull
Thanks! It is fixed now, at least that problem! :) I will select your answer in 10min or so, it won't let me quite yet.
Mr_CryptoPrime
+1  A: 

Anthony Forloney answer is the best

but you can skip the equals, if you want, with intern() function provided by String API.

if( gender1.intern() == "male".intern() ) { ... }

intern

public String intern() Returns a canonical representation for the string object.

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

asela38
if (gender1.intern() == "male") will work as well as string literals are already interned.
cgull