Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String from file vs. input string not evaluated as equal

I’m writing a program for class where I have to find a name in a text file and display some popularity information about the name. However, when I compare the name inputted by the user (in the console) to the name read from the file, it isn’t evaluating to True in my if statement. Why is this? I believe I have appropriately accounted for their typing and for any apparent whitespace. I apologize if this is not the case.

Code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class NamesProcessor {

    public static void main(String[] args) {
        
        System.out.print("This program allows you to search through the \n"
                + "data from the Social Security Administration\n"
                + "to see how popular a particular name has been\n"
                + "since 1900.\n"
                + "\n"
                + "Name?: ");
        
        Scanner console = new Scanner(System.in);
        String name = console.next();
        nameSearch(name.toLowerCase());
        
    }
    
    public static void nameSearch(String name) {
        Scanner readNames = null;
        try {
            File names = new File("src/names");
            readNames = new Scanner(names);
        } catch (FileNotFoundException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
        
        
        while (readNames.hasNext()) {
            String currentName = readNames.next().toLowerCase();
            System.out.println(currentName + " " + name);
            if (currentName == name) {
                System.out.print("Statistics on name \""+ name + "\n");
                int currentYear = 1900;
                while (true) {
                    int popularity = readNames.nextInt();
                    System.out.println(currentYear + ": " + popularity);
                    currentYear += 10;
                    if (currentYear > 2000) {
                        break;
                    }
                }
                break;
            }
            
        }
        
        
    }

}

Output (debugging):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This program allows you to search through the 
data from the Social Security Administration
to see how popular a particular name has been
since 1900.

Name?: Sam
sally sam
0 sam
0 sam
0 sam
0 sam
0 sam
0 sam
0 sam
0 sam
0 sam
0 sam
886 sam
sam sam
//The above line should trigger the if statement in the while loop.
58 sam
69 sam
99 sam
131 sam
168 sam
236 sam
278 sam
380 sam
467 sam
408 sam
466 sam
samantha sam
0 sam
0 sam
0 sam
0 sam
0 sam
0 sam
272 sam
107 sam
26 sam
5 sam
7 sam
samir sam
0 sam
0 sam
0 sam
0 sam
0 sam
0 sam
0 sam
920 sam
0 sam
0 sam
798 sam

>Solution :

It’s because the equal operator compares references in java. You are basically asking "are these the same exact variable?". Fixing this is easy, you just need to use the method isEqual(), equals()or use the compare method for Strings.
Sometimes this doesn’t work for some reason (when comparing fields of some classes).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading