Results 1 to 5 of 5
- 02-19-2013, 02:45 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 60
- Rep Power
- 0
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'); } }
- 02-19-2013, 03:04 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 684
- Rep Power
- 1
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,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-19-2013, 03:17 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 60
- Rep Power
- 0
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.
- 02-19-2013, 03:38 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 684
- Rep Power
- 1
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
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-19-2013, 04:36 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 60
- Rep Power
- 0
Similar Threads
-
public static void main(String... args)???
By SnakeDoc in forum New To JavaReplies: 5Last Post: 01-10-2013, 08:30 AM -
Q?> about public static void main(String args[])
By boblingwide in forum New To JavaReplies: 4Last Post: 03-23-2012, 11:17 AM -
Call a public string variable
By onstock in forum New To JavaReplies: 3Last Post: 11-23-2011, 12:25 PM -
Public static void main (String args[])
By arefeh in forum New To JavaReplies: 12Last Post: 01-28-2010, 11:58 AM -
Err:java.io.IOException: public key ring doesn't start with public key tag
By Deepa in forum New To JavaReplies: 5Last Post: 06-26-2009, 03:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks