Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. package string_methods;
  2. // Starting Template
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class MyStringMethods {
  7.  
  8.     private String myStr = "";
  9.  
  10.     public void readString() {
  11.         // Prompt the user and read in a String from a Scanner class from the keyboard
  12.         // with the nextLine() method and store it in "myStr"
  13.  
  14.         Scanner scan = new Scanner(System.in);
  15.         myStr = scan.nextLine();
  16.         System.out.println("Enter String: ");
  17.  
  18.     }
  19.  
  20.     public void setString(String s) {
  21.         myStr = s;
  22.     }
  23.  
  24.     public int countOccurrences(String s) {
  25.         int count = 0;
  26.         String word[] = myStr.split(" ");
  27.         for (int i = 0; i < word.length; i++) {
  28.             int index = word[i].indexOf(s);
  29.             if (index != -1);
  30.             {
  31.                 count++;
  32.  
  33.             }
  34.         }
  35.         {
  36.             return count;
  37.         }
  38.         // use indexOf and return the number of occurrences of the string "s" in "myStr"
  39.  
  40.     }
  41.  
  42.     public int countOccurrences(char c) {
  43.         int count = 0;
  44.         String word[] = myStr.split(" ");
  45.         for (int j = 0; j < word.length; j++) {
  46.             int index = word[j].indexOf(c);
  47.             if (index != -1) {
  48.                 {
  49.                     count++;
  50.                 }
  51.             }
  52.         }
  53.         return count;
  54.         // use indexOf and return the number of occurrences of the character "c" in
  55.         // "myStr"
  56.     }
  57.  
  58.     public int countUpperCaseLetters() {
  59.         // char[]ch=myStr.toCharArray();
  60.         int count = 0;
  61.         for (int i = 0; i < myStr.length(); i++)
  62.            
  63.         {
  64.  
  65.             if (Character.isUpperCase(myStr.charAt(i))) {
  66.                 count++;
  67.             }
  68.            
  69.         }
  70.         return count;
  71.         // return the number of upper case letters in "myStr"
  72.     }
  73.  
  74.     public int countLowerCaseLetters() {
  75.         char[] ch = myStr.toCharArray();
  76.         int count = 0;
  77.         for (int j = 0; j < myStr.length(); j++)
  78.            
  79.         {
  80.            
  81.             if (Character.isLowerCase(myStr.charAt(j))) {
  82.                 count++;
  83.             }
  84.         }
  85.         // return the number of lower case letters in "myStr"
  86.         return count;
  87.     }
  88.  
  89.     public void printCounts(String s, char c) {
  90.         System.out.println("***************************************");
  91.         System.out.println("Analyzing: myStr=" + myStr);
  92.         System.out.println("Number of Upper case letters=" + countUpperCaseLetters());
  93.         countUpperCaseLetters();
  94.         System.out.println("Number of Lower case letters=" + countLowerCaseLetters());
  95.         countLowerCaseLetters();
  96.         System.out.println("Number of " + s + " is " + countOccurrences(s));
  97.         System.out.println("Number of " + c + " is " + countOccurrences(c));
  98.     }
  99.  
  100.     public static void main(String[] args) {
  101.         MyStringMethods msm = new MyStringMethods();
  102.         msm.readString();
  103.         msm.printCounts("big", 'a');
  104.  
  105.         msm.setString("Parked in a van down by the river bank....The van evan vanished near a lot of other vans");
  106.         msm.printCounts("van", 'a');
  107.  
  108.         MyStringMethods msm2 = new MyStringMethods();
  109.         msm2.setString("the elephant in the room wouldn't budge");
  110.         msm2.printCounts("the", 'i');
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement