Results 1 to 5 of 5
  1. #1
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default public int countOccurrences(char c) & public int countOccurrences(String s)

    Can somebody look at my code in public int countOccurrences(char c) & public int countOccurrences(String s) section and see if they see something wrong? I'm trying to count the number of words in a section with the public int countOccurrences(String s) section and total amount of a single letter in the int countOccurrences(char c) section. Any help would be great!

    Thank you.

    Java Code:
    import java.util.Scanner;
    
    
    public class Strings {
    
        private String myStr="";
        
        public void readString()
        {
        	Scanner scan = new Scanner(System.in);
        	System.out.print("Enter a sentence ");
        	myStr = scan.nextLine();
        }
        public void setString(String s)
        {
            myStr =s;
        }
        public int countOccurrences(String s)
        {
        	int count = 0;
        	int index = myStr.indexOf(s);
        	 
        	while ( index >= 0 ) {
        	   index = myStr.indexOf(s, index+1 );
        	   count++;
        	}
    		return index;
        	
        }
        public int countOccurrences(char c)
        {
        	for (int i = 0; i < myStr.length(); i++)
        	{
        		char c2 = myStr.charAt(i);
     
                }
    		return c;
        	
            }	
        int countUpperCaseLetters()
        {
        	int caps = 0;
    		for (int i=0; i< myStr.length(); i++)
    		{
    			if (Character.isUpperCase(myStr.charAt(i)))
    				caps++;
    		}
    		return caps;
        }
    
        int countLowerCaseLetters()
        {
    		int lowercase = 0;
    		for (int i=0; i< myStr.length(); i++)
    		{
    			if (Character.isLowerCase(myStr.charAt(i)))
    				lowercase ++;
    		}
    		return lowercase;
        }
        public void printCounts(String s, char c)
        {
            System.out.println("***************************************");
            System.out.println("Analyzing: myStr="+myStr);
            System.out.println("Number of "+s + " is "+ countOccurrences(s));
            System.out.println("Number of "+c + " is "+ countOccurrences(c));
            System.out.println("Number of Upper case letters="+ countUpperCaseLetters());
            System.out.println("Number of Lower case letters="+ countLowerCaseLetters());
        }
    
        public static void main(String[] args) {
        	Strings msm = new Strings();
            msm.readString();
            msm.printCounts("big", 'a');
            
            msm.setString("Parked in a van down by the river bank .... The van had was near a lot of other vans");
            msm.printCounts("van", 'a');
            
            Strings msm2 = new Strings();
            msm2.setString("the elephant in the room wouldn't budge");
            msm2.printCounts("the", 'i');
        }
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: public int countOccurrences(char c) & public int countOccurrences(String s)

    Well, upon first glance it looks like you are returning the wrong value in your counOccurences methods for String and char. More specifically, in counOccurrences(char c) you aren't even counting anything or comparing anything to c.

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

  3. #3
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: public int countOccurrences(char c) & public int countOccurrences(String s)

    What should I return then? I've been at this for about 10+ hours now and I'm so confused on this matter.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: public int countOccurrences(char c) & public int countOccurrences(String s)

    Ok, first, look at the countOccurrences(String s). You are returning index. You should return count.
    Second, look at the countOccurrences(char c). You need to do the same thing as for a string. Namely:

    • initialize a count variable.
    • compare c to c2 and update count variable appropriately
    • return count


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

  5. #5
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: public int countOccurrences(char c) & public int countOccurrences(String s)

    You are a life saver! ....

    Thank you Jim!!

Similar Threads

  1. public static void main(String... args)???
    By SnakeDoc in forum New To Java
    Replies: 5
    Last Post: 01-10-2013, 08:30 AM
  2. Q?> about public static void main(String args[])
    By boblingwide in forum New To Java
    Replies: 4
    Last Post: 03-23-2012, 11:17 AM
  3. Call a public string variable
    By onstock in forum New To Java
    Replies: 3
    Last Post: 11-23-2011, 12:25 PM
  4. Public static void main (String args[])
    By arefeh in forum New To Java
    Replies: 12
    Last Post: 01-28-2010, 11:58 AM
  5. Replies: 5
    Last Post: 06-26-2009, 03:03 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
  •