How to search for a name from a text file

cscontrol :

How do I make the scanner find a name case insensitive? For example, if Emily is in the text file.

import java.util.*;
import java.io.*;

public class BabyNames {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner console = new Scanner(System.in);
        System.out.println("** Popularity of a baby name since year 1920 **");
        System.out.print("name? ");
        String name = console.next();

        Scanner input = new Scanner(new File("names.txt"));
        while (input.hasNext()) {
            String search = input.next();
            if (search = name) { // need help here

            }
        }
    }
}
Shar1er80 :

The String class has a equalsIgnoreCase() that you're probably wanting to use.

import java.util.*;
import java.io.*;

public class BabyNames {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner console = new Scanner(System.in);
        System.out.println("** Popularity of a baby name since year 1920 **");
        System.out.print("name? ");
        String name = console.next();

        Scanner input = new Scanner(new File("names.txt"));
        while (input.hasNext()) {
            String search = input.next();
            if (search.equalsIgnoreCase(name)) { 
                // Do something...    
            }
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=103972&siteId=1