我被要求做一个程序,在该程序中我检查用户输入的名称是否在我在我的计算机上创建的文件中。我编写了代码,它不显示任何编译错误,但运行时,布尔表达式始终为false。请帮助我解决这个问题。男孩名字文件中的样本名称是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();
}
}
}
与
==
运营商改变字符串比较明白了非常感谢耆那教先生 – user3339936