Class Name: NameValidation. Write a fragment of code that will read
words from the keyboard until the word exit is entered. For each word
except exit, report whether its first character is equal to its last
character.
import java.util.*;
public class NameValidation {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
boolean done = false;
String str = "exit";
while (!done){
System.out.print("Enter a word: \t");
String str2 = input.next();
str2 = str2.toLowerCase();
int number = str2.length();
int number1 = 0;
String x = str2.substring(number1,1);
String y = str2.substring((number - 1), number);
System.out.println();
if (str.equals(str2)) {
System.out.println("Program is now terminating...");
done = true;
}
else {
done = false;
if (x.equals(y)){
System.out.println("The first character is equal to the last character: " + str2);
}
else {
System.out.println("The first character is not equal to the last character: " + str2);
}
}
}
}
}
No comments:
Post a Comment