Java Spell Check Program in java

Question:

Ruya’s teacher gave her the assignment to practice the spelling of “RAINBOW”. Ruya’s mother helped her in
doing this by spelling each letter as Ruya wrote it in her notebook. If Ruya writes wrong spelling her mother should say “The spelling is wrong”

Sample Input 1:
Enter the first letter:R
Enter the second letter:A
Enter the third letter:I
Enter the fourth letter:N
Enter the fifth letter:B
Enter the sixth letter:O
Enter the seventh letter:W

Sample Output 1:

RAINBOW

Sample Input 2:
Enter the first letter:R
Enter the second letter:E
Enter the third letter:I
Enter the fourth letter:N
Enter the fifth letter:B
Enter the sixth letter:O
Enter the seventh letter:W

Sample Output 2:

The spelling is wrong

CODE:

import java.util.Scanner;
public class Character
{
    public static void main (String[] args) {
        int flag=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the first letter:");
            {if(sc.next().charAt(0)!='R')
                flag++;
        System.out.println("Enter the second letter:");
            if(sc.next().charAt(0)!='A')
                flag++;
        System.out.println("Enter the third letter:");
            if(sc.next().charAt(0)!='I')
                flag++;
        System.out.println("Enter the fourth letter:");
            if(sc.next().charAt(0)!='N')
                flag++;
        System.out.println("Enter the fifth letter:"); 
            if(sc.next().charAt(0)!='B')
                flag++;
        System.out.println("Enter the sixth letter:");
            if(sc.next().charAt(0)!='O')
                flag++;
        System.out.println("Enter the seventh letter:");
            if(sc.next().charAt(0)!='W')
                flag++;
            }
            
        if(flag==0)
        {
            System.out.println("RAINBOW");
        }
        else
        {
            System.out.println("The spelling is wrong");
        }
    }
}

Leave a Comment

Scroll to Top