Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring upper case and lower case in Java

I want to know how to make whatever the user inputs to ignore case in my method:

public static void findPatient() {
    if (myPatientList.getNumPatients() == 0) {
        System.out.println("No patient information is stored.");
    }
    else {
        System.out.print("Enter part of the patient name: ");
        String name = sc.next();
        sc.nextLine();
        System.out.print(myPatientList.showPatients(name));
    }
}
like image 649
Noah Skull Weijian Avatar asked Nov 18 '14 14:11

Noah Skull Weijian


2 Answers

You have to use the String method .toLowerCase() or .toUpperCase() on both the input and the string you are trying to match it with.

Example:

public static void findPatient() {
    System.out.print("Enter part of the patient name: ");
    String name = sc.nextLine();

    System.out.print(myPatientList.showPatients(name));
}

//the other class
ArrayList<String> patientList;

public void showPatients(String name) {
    boolean match = false;

    for(String matchingname : patientList) {
        if (matchingname.toLowerCase().contains(name.toLowerCase())) {
            match = true;
        }
    }
}
like image 91
thenightshines Avatar answered Oct 13 '22 16:10

thenightshines


Use String#toLowerCase() or String#equalsIgnoreCase() methods

Some examples:

    String abc    = "Abc".toLowerCase();
    boolean isAbc = "Abc".equalsIgnoreCase("ABC");
like image 9
dieter Avatar answered Oct 13 '22 16:10

dieter