Lunes, Agosto 19, 2013

LabExer#9

9. 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. For the required loop, use a
a. while statement
b. do-while statement
Sample Output:
Enter a word: mart
The first character is not equal to its last character: mart
Enter a word: tart
The first character is equal to its last character: tart
Enter a word: exit
Program is now terminating…




import java.util.Scanner;

public class NameValidation{
    public static void main(String []args){
        Scanner amante = new Scanner(System.in);
       
        String str = "";
       
            do{
            System.out.println("Enter a word: ");
            str= amante.nextLine();
           
            char FirstCharacter = str.charAt(0);
            char LastCharacter = str.charAt(str.length()-1);
           
            if(str.equals("exit"))      
                System.out.println("Program is now terminating…");
            else if(FirstCharacter == LastCharacter)
               
                System.out.println("The first character is equal to its last character.");
            else
                System.out.println("The first character is not equal to its last character.");
            }
            while(!(str.equalsIgnoreCase("exit")));
           
    }
}

Walang komento:

Mag-post ng isang Komento