0

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.

adiga
  • 34,372
  • 9
  • 61
  • 83
  • 1
    [JavaScript or Java?](https://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java) – evolutionxbox May 17 '22 at 07:06
  • 2
    Fundamentally `main` must be static and any methods that do not need an object to initialized will be `static`. Is it at all possible with your test to initialize an object and then call `howManyVowels`? – Aliics May 17 '22 at 07:13
  • 1
    `howManyVowels` needs to be `static`, at the first place. – tquadrat May 17 '22 at 07:18
  • @tquadrat given the assignment (i.e. to write the code *inside* the method) it looks like `howManyVowels` should be left non-static. – Federico klez Culloca May 17 '22 at 07:20
  • @FedericoklezCulloca – `main()` is static, there is no call to a constructor for the class containing both `main()` and `howManyVowels()` and the latter is called from the first – so `howManyVowels()` needs to be static … – tquadrat May 17 '22 at 07:24
  • @tquadrat *or*, as Aliics pointed out, you could create an instance of whatever class this code is in and call `howManyVowels()` on that instance - so, assuming the content of `main` can be changed, `howManyVowels()` does not *need* to be static. The problem is, both approaches could be correct, but there's not enough context in the question/assignment to decide. – Federico klez Culloca May 17 '22 at 07:28
  • @tquadrat Actually my impression is that OP is testing this code outside the page of the assignment (i.e. locally on their machine or another compiler) and assumed they could copy-paste the example code, while the *real* `main` the assignment would use actually instantiates the class – Federico klez Culloca May 17 '22 at 07:29
  • @Holger I read a bit more on the subject and I retract my statement. Thanks for pointing it out. – Federico klez Culloca May 18 '22 at 14:36

1 Answers1

-1

I can see that you are not using the parameter "sentence" of the "howManyVowels" function , instead you ask the user to enter the sentence :

this is how you can correct that :

    public static void main(String[] args) {

    String sentence = "CsprdkoPurln dhVqq f gul col r jcwk ujd";
  
    System.out.println(howManyVowels(sentence));
   
}

public static String howManyVowels(String sentence) {
    int a=0, e=0, i=0, o=0, u=0, other=0;
    char vowels;

    for (int count=0; count < sentence.length(); count++)
    {
        vowels = sentence.charAt(count);

        switch (vowels)
        {
            case 'a':
                a++;
                break;
            case 'e':
                e++;
                break;
            case 'i':
                i++;
                break;
            case 'o':
                o++;
                break;
            case 'u':
                u++;
                break;
            default:
                other++;
        }
    }
    String str = ("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);               
return str;
}

if you don't want the static context you can create another classe call it Calcul , which contains the "howManyVowels" method and call it in the main classe as bellow:

public static void main(String[] args) {

    String sentence = "CsprdkoPurln dhVqq f gul col r jcwk ujd";
    Calcul.howManyVowels(sentence);
   
}  
FRS
  • 47
  • 4