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

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

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

Edited 3 Years Ago by Dani: Formatting fixed

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

Edited 4 Years Ago by DavidKroukamp: n/a

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.

Comments
Thanks for correcting me for the OP

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..

This article has been dead for over six months. Start a new discussion instead.