letsdoitjava

My String Methods

Jun 17th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. // My String Method Problem,
  2. // Renee Waggoner
  3. // June 24, 2019
  4. // Special Requirements: None
  5.  
  6. package string_methods;
  7. import java.util.Scanner;
  8.  
  9. public class MyStringMethods {
  10.     private String myStr = "";
  11.  
  12.     public void readString() {
  13.  
  14.         // Prompt the user and read in a String and then store
  15.         Scanner keyboard = new Scanner(System.in);
  16.         System.out.println("Enter in the desired String: ");
  17.         myStr = keyboard.nextLine();
  18.  
  19.         keyboard.close();
  20.     }
  21.  
  22.     public void setString(String s) {
  23.         myStr = s;
  24.     }
  25.     // use indexOf and return the number of occurrences of the string "s"
  26.     public int countOccurrences(String s) {
  27.  
  28.         int count = 0;
  29.         int pos = myStr.indexOf(s, 0);
  30.  
  31.         while (pos != -1 && pos<myStr.length()) {
  32.             pos = myStr.indexOf(s, pos+1);
  33.             count++;
  34.         }
  35.         return count;
  36.     }
  37.  
  38.     // use indexOf and return the number of occurrences of the character "c" in "myStr"
  39.     public int countOccurrences(char c) {
  40.  
  41.         int count = 0;
  42.         int pos = myStr.indexOf(c, 0);
  43.  
  44.         while (pos != -1) {
  45.             pos = myStr.indexOf(c, pos+1);
  46.             count++;
  47.         }
  48.         return count;
  49.     }
  50.  
  51.     public int countLowerCaseLetters() {
  52.         int lowerCaseLetters = 0;
  53.         for (int i = 0; i < myStr.length(); i++)
  54.  
  55.             if (Character.isLowerCase(myStr.charAt(i))) {
  56.                 lowerCaseLetters++;
  57.             }
  58.         return lowerCaseLetters;
  59.     }
  60.     int countUpperCaseLetters() {
  61.         int upperCaseLetters = 0;
  62.         for (int i = 0; i < myStr.length(); i++)
  63.             if (Character.isUpperCase(myStr.charAt(i))) {
  64.                 upperCaseLetters++;
  65.             }
  66.         return upperCaseLetters;
  67.     }
  68.  
  69.     public void printCounts(String s, char c) {
  70.  
  71.         System.out.println("***************************************");
  72.         System.out.println("Analyzing: myStr=" + myStr);
  73.         System.out.println("Number of Upper case letters=" + countUpperCaseLetters());
  74.         System.out.println("Number of Lower case letters=" + countLowerCaseLetters());
  75.         System.out.println("Number of " + s + " is " + countOccurrences(s));
  76.         System.out.println("Number of " + c + " is " + countOccurrences(c));
  77.     }
  78.  
  79.     public static void main(String[] args) {
  80.  
  81.         MyStringMethods msm = new MyStringMethods();
  82.         msm.readString();
  83.         msm.printCounts("big", 'a');
  84.         msm.setString("Parked in a van down by the river bank .... The vanevan vanished near a lot of other vans");
  85.         msm.printCounts("van", 'a');
  86.         MyStringMethods msm2 = new MyStringMethods();
  87.         msm2.setString("the elephant in the room wouldn't budge");
  88.         msm2.printCounts("the", 'i');
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment