使用从扫描仪获取的字符串不起作用?

发布于 2024-09-29 02:03:08 字数 1602 浏览 0 评论 0原文

当我运行程序并输入排名时性别 它对我大喊大叫并告诉我这是一个无效的性别。我不明白为什么如果我在控制台中输入“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

会发光的星星闪亮亮i 2024-10-06 02:03:08

要比较字符串,您需要使用.equals() 因此,将所有与 == 的字符串比较更改为使用 .equals() 相反。

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

会变成,

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

等等。

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.

拿命拼未来 2024-10-06 02:03:08

'==' 运算符测试对字符串引用的等效性,而不是字符串内容的等效性,因此您的代码没有给出预期结果。

尝试使用表格

if (gender1.equals("female"))

等代替。

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.

丢了幸福的猪 2024-10-06 02:03:08

Anthony Forloney 的答案是最好的,

但如果您愿意,您可以使用 String API 提供的 intern() 函数跳过 equals。

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

实习生

公共字符串实习生()
返回字符串对象的规范表示。

字符串池最初是空的,由 String 类私有维护。

当调用 intern 方法时,如果池中已包含由 equals(Object) 方法确定的等于此 String 对象的字符串,则返回池中的字符串。否则,将此 String 对象添加到池中,并返回对此 String 对象的引用。

因此,对于任意两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文