Home > Software engineering >  String from file vs. input string not evaluated as equal
String from file vs. input string not evaluated as equal

Time:05-04

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):

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

CodePudding user response:

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).

  • Related