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