In: Computer Science
JAVA
Write a program that will compare two names. The program prompts the user to enter two names for a comparison. If the names are same, the program states that. If the names are different, the program converts both names to UPPERCASE, and compares then again. If they are equal, the programs displays a message stating that names are equal if CASE is ignored. Otherwise, the program prints names with a message that names are not equal. Check also if there is a way to compare strings while ignoring case (upper or lower)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new
Scanner(System.in);
System.out.println("Enter two
names");
String name1 = s.nextLine(); //
gets name1
String name2 = s.nextLine(); //
gets name2
if(name1.equals(name2)) { // checks
whether the names are same
System.out.println("Names are
same");
}else
if(name1.toUpperCase().equals(name2.toUpperCase())){ // conerts
into uppercase and checks
System.out.println("Names are
same");
}else
if(name1.equalsIgnoreCase(name2)){ // ignores the case and
checks
System.out.println("Names are
same");
}else{ // gets executed if names
are not same
System.out.println("The names
"+name1+" and "+name2+" are different");
}
}
}