Results 1 to 5 of 5
  1. #1
    Cinnic is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default letter counter prgm error

    I am trying to write a code that ask the user to input a string and then enter a character, which is to be counted & displayed. I think I am pretty close but I haven't been able to complete. My while statement is incorrect & I may need another line of code or two. Would really appreciate some help. Thanks

    Java Code:
    import java.util.Scanner;
    
    
    public class LCounter 
    {
      public static void main (String [] args) 
      {
        
        //delcare a scanner & attach it to kbrd
        Scanner kbrd = new Scanner(System.in);
        
        //declare input variables
        String input;   //hold users string
        String letter;  //hold character
        int counter=0;
        int pos=0;
            
        //ask user for a string to store
        System.out.println("Enter a string.");
        input=kbrd.next();
        
        //ask user for a character and store
        System.out.println("Enter a character.");
        letter=kbrd.next();
            
        //
        while (pos<input.length())
        { char  l = letter.charAt(pos);
          if (l==pos) counter++;
          pos++;
        }
        
        
        
        //display results
        System.out.println(letter+" appears "+counter+" times");
        
      } 
       
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: letter counter prgm error

    First, it appears you are iterating thru the input letter. You want to iterate thru the string.

    Then you need to compare characters, not positions.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Cinnic is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: letter counter prgm error

    Thanks Jim for such a quick response. However, I have been coding for a little over a week and if you could show me with an example, it might make more sense to me. Please & thanks!

  4. #4
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default Re: letter counter prgm error

    On line 28 you are getting the charAt of letter. This is incorrect. You should be getting the charAt of your input string called "input". Then compare that to the char you are searching for. You will need to get this prior to the while loop. Hint, it should only be one char so do letter.charAt(0). Then compare the two chars in the while loop.

  5. #5
    Cinnic is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: letter counter prgm error

    Thanks for breaking it down a little further. It definitely helped a lot and now it works!
    Java Code:
    //load Scanner 
    import java.util.Scanner;
    
    
    public class LCounter 
    {
      public static void main (String [] args) 
      {
        
        //delcare a scanner & attach it to kbrd
        Scanner kbrd = new Scanner(System.in);
        
        //declare input variables
        String input;   //hold users string
        String letter;  //hold character
        int counter=0;  //accumulator
        int pos=0;      //comparison progressor
            
        //ask user for a string to store
        System.out.println("Enter a string.");
        input=kbrd.nextLine();
        
        //ask user for a character and store
        System.out.println("Enter a character.");
        letter=kbrd.next();
        
        //selects 1st character of Stringletter
        char x=letter.charAt(0);
             
        //loop for comparing Stringletter to Stringinput
        while (pos<input.length())        //executes until all character are compared
          { char  l = input.charAt(pos);  //selects which character of Stringinput to compare 
            if (l==x) counter++;          //compare characters, if match add 1 to counter
            pos++;                        //increase int pos every loop
           }
        
        
        //display results
        System.out.println(letter+" appears "+counter+" times");
        
      } 
       
    }

Similar Threads

  1. Letter counter homework problem
    By Djgnl in forum New To Java
    Replies: 15
    Last Post: 09-24-2011, 02:37 AM
  2. Error in Counter Code
    By DragonGhola in forum New To Java
    Replies: 1
    Last Post: 07-12-2011, 04:20 PM
  3. Letter counter program. having trouble understanding it.
    By silverglade in forum New To Java
    Replies: 17
    Last Post: 05-21-2011, 01:06 PM
  4. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  5. Counter
    By ks1615 in forum New To Java
    Replies: 6
    Last Post: 02-20-2009, 03:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •