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");
      }
}

here's your problem:

userString = userString.toLowerCase();

you set your String so, that it consist solely out of lowercases, so ... no, it doesn't work, since the char you compare it to, is still upercase

Updated code below.
Took out some unneeded code and still having problem with program to NOT recognize lower or upper case when use imputs letter from the userChar code. How do I get java to recognize or should say not recognize small or upper case and still execute the program correcly.

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();






      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 " + userString);
      }
}

Updated code below.
Took out some unneeded code and still having problem with program to NOT recognize lower or upper case when use imputs letter from the userChar code. How do I get java to recognize or should say not recognize small or upper case and still execute the program correcly.


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();


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 " + userString);
}
}

try using:

if(userChar.equalsIgnoreCase(userString.charAt(i))){}

-this will make your search case insensitive

try using:

if(userChar.equalsIgnoreCase(userString.charAt(i))){}

-this will make your search case insensitive

not really.
he's using char primitives, so it won't have an equals method. the Character class also doesn't have an equalsIgnoreCase

possible solution:
keep the
userString.toLowerCase();
but then you'll also have to replace the char witht he lowercase version.

commented: Thanks for correcting me for the OP +5

not really.
he's using char primitives, so it won't have an equals method. the Character class also doesn't have an equalsIgnoreCase

possible solution:
keep the
userString.toLowerCase();
but then you'll also have to replace the char witht he lowercase version.

yeah sorry i found that out while replying to another thread. my mistake..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.