lundi 27 mars 2017

Using equalsIgnoreCase in if statement and while loop

    String wrd;

    do
    {
        wrd = JOptionPane.showInputDialog("Enter a word");
        if (!wrd.equalsIgnoreCase("last"))
        {
            if (wrd.contains("s") || wrd.contains("S"))
            {
                System.out.println(wrd + " does contain 's' or 'S'");
            }
            else
            {
                System.out.println(wrd + " doesn't contain 's' or 'S'");
            }
        }
    }
    while (!wrd.equalsIgnoreCase("last"));
    System.out.println("Program Ended");

This program basically allows a user to enter any word in until he/she types the word "last".

If the word contains the letter 's' it should display the word + does contain 's' or 'S' else it should display word + doesn't contain 's' or 'S'.

The program works but I need to change the if statement so that it recognises lower and uppercase 's' using the equalsIgnoreCase method instead of using

    wrd.contains("s") || wrd.contains("S")

Aucun commentaire:

Enregistrer un commentaire