letsdoitjava

My String Methods

Jun 23rd, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 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. // public meaning visible, void meaning no return value
  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.     // allowing access further down in this program
  26.     public int countOccurrences(String s) {
  27.         int count = 0;
  28.         // indexOf returns the index of the string
  29.         int pos = myStr.indexOf(s, 0);
  30.         // use indexOf and return the number of occurrences of the string "s" in "myStr" method
  31.        
  32.         while (pos != -1 && pos<myStr.length()) {
  33.             // indexOf returns the index of the string
  34.             pos = myStr.indexOf(s, pos+1);
  35.             count++;
  36.         }
  37.         return count;
  38.     }
  39.  
  40.         public int countOccurrences(char c) {
  41.         int count = 0;
  42.         int pos = myStr.indexOf(c, 0);
  43.  
  44.         while (pos != -1) {
  45.             // use indexOf and return the number of occurrences of the character "c" in "myStr" method
  46.             pos = myStr.indexOf(c, pos+1);
  47.             count++;
  48.         }
  49.         return count;
  50.     }
  51.         // Initialize method count of lower case letters, public for class and subclass access
  52.     public int countLowerCaseLetters() {
  53.         int lowerCaseLetters = 0;
  54.         for (int i = 0; i < myStr.length(); i++)
  55.             // method, reading for lower case letter, charAt extracts characters
  56.             if (Character.isLowerCase(myStr.charAt(i))) {
  57.                 lowerCaseLetters++;
  58.             }
  59.         return lowerCaseLetters;
  60.     }
  61.     int countUpperCaseLetters() {
  62.         int upperCaseLetters = 0;
  63.         // instructions for counting upper case letters
  64.         for (int i = 0; i < myStr.length(); i++)
  65.            
  66.             if (Character.isUpperCase(myStr.charAt(i))) {
  67.                 upperCaseLetters++;
  68.             }
  69.         return upperCaseLetters;
  70.     }
  71.     // public means can be called from other objects of other types, void means that it has no return value
  72.     public void printCounts(String s, char c) {
  73.         // calling methods
  74.         System.out.println("***************************************");
  75.         System.out.println("Analyzing: myStr=" + myStr);
  76.         System.out.println("Number of Upper case letters=" + countUpperCaseLetters());
  77.         System.out.println("Number of Lower case letters=" + countLowerCaseLetters());
  78.         System.out.println("Number of " + s + " is " + countOccurrences(s));
  79.         System.out.println("Number of " + c + " is " + countOccurrences(c));
  80.     }
  81.     // static means that the method is associated with the class, not a specific instance (obj) of that class
  82.     public static void main(String[] args) {
  83.        
  84.         // introduce new method, allocating memory
  85.         MyStringMethods msm = new MyStringMethods();
  86.         msm.readString();
  87.         msm.printCounts("big", 'a');
  88.         msm.setString("Parked in a van down by the river bank .... The vanevan vanished near a lot of other vans");
  89.         msm.printCounts("van", 'a');
  90.         MyStringMethods msm2 = new MyStringMethods();
  91.         msm2.setString("the elephant in the room wouldn't budge");
  92.         msm2.printCounts("the", 'i');
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment