Alright, here's my code...

import java.util.Scanner;

 public class Letter
 {
   public static void main(String[] args)
   {
      
     //needed for scanner class
      Scanner kb = new Scanner(System.in);
      
      int charCount = 0;
      
      // get users string
      System.out.println("Please enter a string: ");
      String userString = kb.nextLine();
      // get users character
      System.out.println("Please enter a character: ");
      String userChar = kb.nextLine();
      // tell the user what the program does
      System.out.println("****This program will return how many times" +
                          " the character you entered showed up in" +
                          " the string you entered.****");
                     
      for(int i= 0;i<userString.length();i++)
      {
              if (userString.charAt(i) == userChar)
                  {
                  charCount++;
                  }
      }
      System.out.println("The specified character '" + userChar +
                          "' is inside the string '" + userString +
                          "'" + charCount +   "' times.);            
    }

You can probably already see my error, but i'm getting incomparable types, obviously a string and a char. Any way to fix this?

Recommended Answers

All 3 Replies

here you go, as you read whole line you also get return character and you need to get rid of it, there are various way how to do it but as you already use charAt() I used again to just get first (zero position) character in string. this is not bullet prove as somebody can press space and then add character so think about it

import java.util.*;

public class Letter
 {
   public static void main(String[] args)
   {
      
     //needed for scanner class
      Scanner kb = new Scanner(System.in);
      
      int charCount = 0;
      
      // get users string
      System.out.print("Please enter a string: ");
      String userString = kb.nextLine();
      // get users character
      System.out.print("Please enter a character: ");
      char userChar = kb.nextLine().charAt(0);
      // tell the user what the program does
      System.out.println("\n****This program will return how many times" +
                          " the character you entered showed up in" +
                          " the string you entered.****");
                     
      for(int i= 0;i<userString.length();i++)
      {
              if (userString.charAt(i) == userChar)
                  {
                  charCount++;
                  }
      }
      System.out.println("\nThe specified character " +"\"" + userChar +
                          "\" is inside the string " + userString +
                          " " + charCount +   " times.\n");            
    }
}

here you go, as you read whole line you also get return character and you need to get rid of it, there are various way how to do it but as you already use charAt() I used again to just get first (zero position) character in string. this is not bullet prove as somebody can press space and then add character so think about it

import java.util.*;

public class Letter
 {
   public static void main(String[] args)
   {
      
     //needed for scanner class
      Scanner kb = new Scanner(System.in);
      
      int charCount = 0;
      
      // get users string
      System.out.print("Please enter a string: ");
      String userString = kb.nextLine();
      // get users character
      System.out.print("Please enter a character: ");
      char userChar = kb.nextLine().charAt(0);
      // tell the user what the program does
      System.out.println("\n****This program will return how many times" +
                          " the character you entered showed up in" +
                          " the string you entered.****");
                     
      for(int i= 0;i<userString.length();i++)
      {
              if (userString.charAt(i) == userChar)
                  {
                  charCount++;
                  }
      }
      System.out.println("\nThe specified character " +"\"" + userChar +
                          "\" is inside the string " + userString +
                          " " + charCount +   " times.\n");            
    }
}

Works now, thanks a lot. As for error correction, our professor isn't huge on that, so this should be sufficient. Now to do this same program, except using the text from a file =)

not much difference

open file
read line by line
and do your character count
then don't forget to close file once finished with reading

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.