I know I'm doing something stupid, but I cannot figure how to fix it.
The issue is inside the private method removeVowels particulry when using the vowels method.
The compiler gives
non-static variable vowels cannot be referenced from a static context
Here is my code:
public class RecursionHW2 { String vowels; // Part (A) First way public static int upperCase(String myString){ return upperCaseChecker(myString , 0 ); } public static int upperCaseChecker(String myString, int index){ int inc; //My Base Code if(myString.length() <= index) return 0; if(Character.isUpperCase(myString.charAt(index)) == true) inc= 1; else inc= 0; return inc+upperCaseChecker(myString,index+1); } // First way of Solving part (B) public static int count(String str, char a) { if (str.length() == 0) return 0; else if (str.charAt(0) == a) return 1 + count(str.substring(1, str.length()), a); else return count(str.substring(1, str.length()), a); } //Second way of solving part (B) public static int anotherCount(String myString, char myWord) { return anotherCount(myString, myWord, 0); } public static int anotherCount(String myString, char myWord, int index) { int inc; if (index >= myString.length()) { return 0; } if (myString.charAt(index) == myWord) inc =1; else inc = 0; return inc + anotherCount(myString, myWord, index+1); } // part (C) solving public Boolean isSorted(int[] a, int n) { if(n == 0 || n == 1) return true; else return isSorted(a, n, 1); } private Boolean isSorted(int[] a, int n, int cur) { if(cur == n) return true; if(a[cur - 1] <= a[cur]) return isSorted(a, n, cur+1); else return false; } //part (D) Solving public static String removeVowels(String myString) { return removeVowels(myString, ""); } private static String removeVowels(String myString, String t) { if(myString.length() == 0) return t; if(vowels.contains(myString.charAt(0) + "")) return removeVowels(myString.substring(1), t); else return removeVowels(myString.substring(1), t + myString.charAt(0)); } public static void main(String[] args){ //I've wrote 2 ways to solve the Second Recursive Q2 System.out.println("Method 1: Number of Occurence " + count("Hello This is Mohammad Fadin",'o')); // System.out.println("Method 2: Number of Occurence "+ anotherCount("Hello This is Mohammad Fadin",'o')); String s1 = "Hello WorlDD"; System.out.println("Number of Upper Cases " + upperCase(s1)); String s2 = "Hello"; System.out.println("After Vowels Removed " + removeVowels(s2)); } }