如何使扫描仪发现名称不区分大小写?例如,如果Emily在文本文件中。
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
}
}
}
}
答案 0 :(得分:1)
if (search.contains("Emily")) {}
if (search.indexOf("Emily") > -1) {}
答案 1 :(得分:1)
String
类具有一个您可能想使用的equalsIgnoreCase()
。
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...
}
}
}
}