switch ignore case java code example

Example 1: java switch case

switch(x){
	case(0):		//if x == 0
    	//do some stuff
    	break;
    //add more cases
  default:			//when x does not match any case
    //do some stuff
    break;
}

Example 2: how to use ignore case in java

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;
        }
    }
}

Example 3: switch case less than java

switch (lessThan)
	{
    
    //Less than cannot techinically be added so we use the numbers less than those numeberss
		case 1:
		case 2:
		case 3:
		System.out.println("This name has 3 or less characters");
		break;

		case 4:
		System.out.println("This name has 4 characters");
		break;
    
	}