Screenshot of my question I'm answering
I'm running into this problem with my switch method, where the method which has this switch needs to be made static. It runs when I remove the static from the Main method, no errors its happy UNTIL I go to run it which I can't.
This is my code file, I don't know why it runs with errors that way, I'm absolutely stuck at what is going on.
public static void main(String[] args) {
String sentence = "CsprdkoPurln dhVqq f gul col r jcwk ujd";
System.out.println(howManyVowels(sentence));
}
public String howManyVowels(String sentence) {
String userString;
int a=0, e=0, i=0, o=0, u=0, other=0;
char vowels;
Scanner scan = new Scanner (System.in);
userString = scan.nextLine();
for (int count=0; count < userString.length(); count++)
{
vowels = userString.charAt(count);
switch (vowels) {
case 'a' -> a++;
case 'e' -> e++;
case 'i' -> i++;
case 'o' -> o++;
case 'u' -> u++;
default -> other++;
}
}
return ("Number of each lowercase vowel in the string: \n") + ("a: " + a) + ("\n" + "e: " + e) + ("\n"+"i: " + i) + ("\n"+"o: " + o) + ("\n"+"u: " + u) + ("\n"+"other " + other);
}
}
The error I run into is just not being able to under the code. I honestly don't know what is wrong with it.
The program it runs through apparently uses the old switch method. Which I haven't heard about since what I've written is all I know for what a switch looks like. Originally I had it formatted as,
switch (vowels)
{
case 'a':
a++;
break;
case 'e':
e++;
break;
case 'i':
i++;
break;
case 'o':
o++;
break;
case 'u':
u++;
break;
default:
other++;
}
Which I couldn't get to run unless the Main method was not static, or I made the howManyVowels static which was not in the question criteria, thus being slapped back with an error + being wrong.