使用从扫描仪获取的字符串不起作用?
当我运行程序并输入排名时性别 它对我大喊大叫并告诉我这是一个无效的性别。我不明白为什么如果我在控制台中输入“male”,它不等于字符串“male”?请向我解释为什么这不起作用,也许还有一些关于如何修复它的建议?谢谢!
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);
}
}
}
}
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);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要比较
字符串
,您需要使用.equals()
因此,将所有与==
的字符串比较更改为使用.equals()
相反。会变成,
等等。
To compare
Strings
, you need to use.equals()
so change all of your String comparisons with==
to use.equals()
instead.would become,
So on and so forth.
'==' 运算符测试对字符串引用的等效性,而不是字符串内容的等效性,因此您的代码没有给出预期结果。
尝试使用表格
等代替。
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
etc. instead.
Anthony Forloney 的答案是最好的,
但如果您愿意,您可以使用 String API 提供的 intern() 函数跳过 equals。
Anthony Forloney answer is the best
but you can skip the equals, if you want, with intern() function provided by String API.