What's wrong with my code? Counting letters in a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ccarter45
    New Member
    • Mar 2008
    • 10

    What's wrong with my code? Counting letters in a string

    I need to count the number of letters in a string, disregarding case. It needs to use these two methods. What's wrong?
    Code:
    import java.util.Scanner;
    
    public class CountLetters
    {
      public static void main(String[] args)
      {
        Scanner input;
    
        input = new java.util.Scanner(System.in);
      
      String s;
      int count = 0;
        
        
        System.out.print( "Enter a string: " );
        s = input.nextLine(); 
        count = s.length();
        
        
        System.out.println("The number of letters in the string is " +count+".");
      }
    public static int count (String s) 
    {
      int count = 0;
      count = s.length();
    for(int i = 0; i < s.length(); i++) {
         if(s.charAt(i) == 'i') 
         {
              count++;
         }
    }}}
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Your code doesn't even compile and your compiler can tell you exactly why it
    doesn't compile; try it.

    kind regards,

    Jos

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      You are defining "count" in your main method as int.
      But you also have a method called "count". This is bad practice and this is why it does not do what you expect. Always use different names!

      You are assigning to your variable "count" the length of your input string (why?), but you are not calling your method "count", which would have brought back the desired number of letters "i" inside your input string.

      This code does not look as if you have written it your own.
      It looks as if someone has intentionally modified working code for some students to correct it. The students should understand the difference between
      method and variable and between variable "i" and character "i". If they understand, they are able to correct the code.

      If I am right, then helping you here may have negative influence to your learn success and wasting my time. Your teacher is paid for explaining you all things, whereas posting such code here prohibits me from helping people who really need help with their own code.

      Originally posted by ccarter45
      I need to count the number of letters in a string, disregarding case. It needs to use these two methods. What's wrong?
      Code:
      import java.util.Scanner;
      
      public class CountLetters
      {
        public static void main(String[] args)
        {
          Scanner input;
      
          input = new java.util.Scanner(System.in);
        
        String s;
        int count = 0;
          
          
          System.out.print( "Enter a string: " );
          s = input.nextLine(); 
          count = s.length();
          
          
          System.out.println("The number of letters in the string is " +count+".");
        }
      public static int count (String s) 
      {
        int count = 0;
        count = s.length();
      for(int i = 0; i < s.length(); i++) {
           if(s.charAt(i) == 'i') 
           {
                count++;
           }
      }}}

      Comment

      • radhikams
        New Member
        • Jan 2008
        • 49

        #4
        Originally posted by ccarter45
        I need to count the number of letters in a string, disregarding case. It needs to use these two methods. What's wrong?
        Code:
        import java.util.Scanner;
        
        public class CountLetters
        {
          public static void main(String[] args)
          {
            Scanner input;
        
            input = new java.util.Scanner(System.in);
          
          String s;
          int count = 0;
            
            
            System.out.print( "Enter a string: " );
            s = input.nextLine(); 
            count = s.length();
            
            
            System.out.println("The number of letters in the string is " +count+".");
          }
        public static int count (String s) 
        {
          int count = 0;
          count = s.length();
        for(int i = 0; i < s.length(); i++) {
             if(s.charAt(i) == 'i') 
             {
                  count++;
             }
        }}}
        there is only one mistake in the count(String s) which has to return something.. If you can check it then your code runs properly

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by radhikams
          there is only one mistake in the count(String s) which has to return something.. If you can check it then your code runs properly
          And then the method returns the length of its String parameter plus the number
          of times the lowercase 'i' occurs in that String. Not quite what the user wanted.

          kind regards,

          Jos

          Comment

          • radhikams
            New Member
            • Jan 2008
            • 49

            #6
            Originally posted by JosAH
            And then the method returns the length of its String parameter plus the number
            of times the lowercase 'i' occurs in that String. Not quite what the user wanted.

            kind regards,

            Jos
            Im sorri Jos im mistaken..Thank you for correcing me..But now actually im confused taht counting letters means in general the no. of letters present in the string or no. of times a letter is present in that string?????

            Comment

            • Navdip
              New Member
              • Mar 2008
              • 22

              #7
              Originally posted by ccarter45
              I need to count the number of letters in a string, disregarding case. It needs to use these two methods. What's wrong?
              Code:
              import java.util.Scanner;
              
              public class CountLetters
              {
                public static void main(String[] args)
                {
                  Scanner input;
              
                  input = new java.util.Scanner(System.in);
                
                String s;
                int count = 0;
                  
                  
                  System.out.print( "Enter a string: " );
                  s = input.nextLine(); 
                  count = s.length();
                  
                  
                  System.out.println("The number of letters in the string is " +count+".");
                }
              public static int count (String s) 
              {
                int count = 0;
                for(int i = 0; i < s.length(); i++) {
                   if(s.charAt(i) == 'i') 
                   {
                        count++;
                   }
              }}}

              Hi...
              in your code in which u calculating the length of string, u were not calling your function...and also not returned calculated value.You can use the follwing code it will really help you.
              ---------------------------------------------------------------------------------------
              I removed the spoonfeeding code; we don't do that here because the OP won't
              learn anything from copying/pasting code. Don't supply code anymore.

              kind regards,

              Jos (moderator)

              Comment

              • pralu
                New Member
                • Mar 2008
                • 34

                #8
                <Code removed by MODERATOR - Please read our Posting Guidelines.>


                hope this will work for u....... n ya u r moving more inner to the scanner package.... specify it import java.util.*;

                Comment

                Working...