Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing string taken from Scanner not working?
    primarykey
    data
    text
    <p>When I run the program and enter a rank &amp; 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! </p> <pre><code>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 &lt;= 0 || rank &gt; 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); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload