fork download
  1. import java.util.Scanner;
  2. public class MyStringMethods{
  3.  
  4. private String myStr="";
  5.  
  6.  
  7. Scanner sc = new Scanner(System.in);
  8.  
  9. public void readString()
  10. {
  11. System.out.println("Enter in the desired string: ");
  12. myStr = sc.nextLine();
  13. }
  14. public void setString(String s)
  15. {
  16. myStr = s;
  17. }
  18. public int countOccurrences (String s)
  19. {
  20. int index = 0;
  21. int start = 0;
  22. int count = 0;
  23. do {
  24. index = myStr.indexOf( s, start);
  25. count += 1;
  26.  
  27. if (index >= 0)
  28. {
  29. start = index + 1;
  30. }
  31. } while (index >= 0);
  32.  
  33. return count;
  34. }
  35.  
  36. public int countOccurrences (char c)
  37. {
  38. int index1 = 0;
  39. int start1 = 0;
  40. int count1 = 0;
  41.  
  42. do {
  43. index1 = myStr.indexOf( c, start1);
  44. count1 += 1;
  45. if (index1 >= 0)
  46. {
  47. start1 = index1 + 1;
  48.  
  49.  
  50. }
  51. } while (index1 >= 0);
  52.  
  53. return count1;
  54.  
  55. }
  56.  
  57. private static long countUpperCase(String myStr) {
  58. return myStr.chars().filter((s)->Character.isUpperCase(s)).count();
  59. }
  60.  
  61. private static long countLowerCase(String myStr) {
  62. return myStr.chars().filter((s)->Character.isLowerCase(s)).count();
  63. }
  64. public void printCounts(String s, char c)
  65. {
  66. System.out.println("***************************************");
  67. System.out.println("Analyzing: myStr="+ myStr);
  68. System.out.println("Number of Upper case letters="+ countUpperCase(myStr));
  69. System.out.println("Number of Lower case letters="+ countLowerCase(myStr));
  70. System.out.println("Number of "+ s + " is "+ countOccurrences(s));
  71. System.out.println("Number of "+ c + " is "+ countOccurrences(c));
  72. }
  73. public static void main(String[] args) {
  74. MyStringMethods msm = new MyStringMethods();
  75. msm.readString();
  76. msm.printCounts("big", 'a');
  77.  
  78. msm.setString("Parked in a van down by the river bank .... The van evan vanished near a lot of other vans");
  79. msm.printCounts("van", 'a');
  80.  
  81. MyStringMethods msm2 = new MyStringMethods();
  82. msm2.setString("the elephant in the room wouldn't budge");
  83. msm2.printCounts("the", 'i');
  84. }
  85. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Aardvarks get big and bigger as You feed them big and bigger ants who Feed on apples. 
compilation info
Main.java:2: error: class MyStringMethods is public, should be declared in a file named MyStringMethods.java
public class MyStringMethods{	
       ^
1 error
stdout
Standard output is empty