Sample input 1: Enter Ist letter:J Enter 2nd letter:A Enter 3rd letter :V Enter 4th letter:A Sample output 1: JAVA
Sample input 2: Enter Ist letter:J Enter 2nd letter:A Enter 3rd letter :A Enter 4th letter:V Sample output 2: Wrong spelling
Sample input 1: Enter Ist letter:J Enter 2nd letter:A Enter 3rd letter :V Enter 4th letter:A Sample output 1: JAVA
Sample input 2: Enter Ist letter:J Enter 2nd letter:A Enter 3rd letter :A Enter 4th letter:V Sample output 2: Wrong spelling
Try the following code.
public class Test {
public static void main(String[] args) {
String finalString = "";
Scanner sc = new Scanner(System.in);
for (int i = 1; i < 5; i++) {
System.out.println("Enter " + i + "letter");
String letter = sc.nextLine();
finalString = finalString + letter;
}
if (!finalString.equals("JAVA")) {// you can test it with any string by replacing java
System.out.println("Wrong spelling");
} else {
System.out.println(finalString);
}
}
}