Always returns false

user3339936

I am asked to do a program in which I have check if the names entered by the user is in the file I have created on my computer. I wrote the code and it does not show any compilation error but when it runs the boolean expression is always false. Please do help me out with this. The sample names on the boy names file is Martin and on the girl names is 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();
        }

    }
}
Nambi

Why you are comparing the String with == operator change with equals()

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related