I am working on a nested control structure program that will, when user imputs any letter, whether it be upper or lower case will count the number of times the letter appears in the sentence entered by the user. In the code below when the user enters lower case letter to the first output question, the out put is correct, but when an upper case letter is entered in the first output question the program doesnt execute correctly, it doesn't see the letter.
import java.util.Scanner;
public class CountSent
{
public static void main(String[]args)
{
Scanner cs = new Scanner(System.in);
int charCount = 0;
System.out.print("Enter a letter: ");
char userChar = cs.nextLine().charAt(0);
System.out.print("Enter a sentence: ");
String userString = cs.nextLine();
userString = userString.toLowerCase();
userString = userString.substring(0, userString.length()-1);
for (int i = 0; i < userString.length();i++)
{
if (userString.charAt(i) == userChar)
{
charCount++;
}
}
System.out.println("\n\n" + charCount + " words contain the letter "+ userChar +".");
System.out.println("The character " + userChar + " appears " + charCount +" times in the sentence");
}
}