【问题标题】:Always returns false [duplicate]总是返回 false [重复]
【发布时间】:2014-03-01 19:13:28
【问题描述】:

我被要求执行一个程序,在该程序中我检查用户输入的名称是否在我在计算机上创建的文件中。我编写了代码,它没有显示任何编译错误,但是当它运行时,布尔表达式总是错误的。请帮我解决这个问题。男孩名字文件上的示例名字是 Martin,女孩名字上的示例名字是 Emily。

import java.util.*;
import java.io.*;
public class nameSearch1
{
    public boolean readingGirlNames () throws IOException
    {
        String[] girlNames = new String [200];
        int i = 0;
        File file = new File ("GirlNames.txt");
        Scanner inputFile = new Scanner(file);

        while(inputFile.hasNext() && i < girlNames.length )
        {
            girlNames [i] = inputFile.nextLine();
            i++;
        }
        inputFile.close();

        Scanner keyboard=new Scanner(System.in);
        boolean girlName = false ;
        nameSearch object4 = new nameSearch();
        System.out.println(" Enter the Girl Name you wish to see : ");

        String nameCheckGirl = keyboard.nextLine();

        for ( int index = 0 ; index < 200 ; index ++ )
        {
            if( nameCheckGirl == girlNames[index])
            { 
                girlName = true;
            }

        }
        return girlName;
    }

    public boolean readingBoyNames () throws IOException
    {
        String[] boyNames = new String [200];
        int i = 0;
        File file = new File ("BoyNames.txt");
        Scanner inputFile = new Scanner(file);

        while(inputFile.hasNext() && i < boyNames.length )
        {
            boyNames [i] = inputFile.nextLine();
            i++;

        }
        inputFile.close();




        nameSearch object2 = new nameSearch ();
        Scanner keyboard=new Scanner(System.in);
        boolean boyName = false ;

        System.out.println(" Enter the Boy Name you wish to see : ");

        String nameCheckBoy = keyboard.nextLine();

        for ( int index = 0 ; index < 200 ; index ++ )
        {
            if( nameCheckBoy == boyNames[index])
            { 
                boyName = true;
            }

        }
        return boyName;
    }

    public static void main(String[]Args)
    {
        Scanner keyboard = new Scanner(System.in);

        nameSearch1 object1 = new nameSearch1 ();

        try
        {
            System.out.println(" The search result for the Girl's name is : " + object1.readingGirlNames ());
        }
        catch (IOException ioe)
        {
            System.out.println(" Exception!!! ");

            ioe.printStackTrace();
        }

        try
        {
            System.out.println(" The search result for the Boy's name is : " + object1.readingBoyNames ());
        }
        catch (IOException ioe)
        {
            System.out.println(" Exception!!! ");

            ioe.printStackTrace();
        }

    }
}

【问题讨论】:

  • 知道了,非常感谢 Jain 先生

标签: java methods output


【解决方案1】:

为什么要将字符串与== 运算符更改与equals() 进行比较

if( nameCheckBoy.equals(boyNames[index]))
            { 
                boyName = true;
            }

【讨论】:

  • 知道了,谢谢纳拉亚南先生
  • @user3339936 很高兴为您提供帮助:)
猜你喜欢
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-08
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
相关资源
最近更新 更多