-1

I have this class that I'm trying to use to find the number of vowels and number of consonants in a string.

import java.lang.String;
    public class vowelsAndConsonantsClass
    {
      private int vowels;
      private int consonants;
      public vowelsAndConsonantsClass(int vwls, int cons)
      {
        vowels = vwls;
        consonants = cons;
      }
        public void numberOfVowels(String str)
        {
          int vwls = 0;
          for(int i=0; i>str.length(); i++)
          {
            if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u' || str.charAt(i) == 'y');
            vwls++;
          }
        }//end numberOfVowels 
        public void numberOfConsonants(String str)
        {
          int cons = 0;
          for(int i=0; i>=str.length(); i++)
          {
            if(str.charAt(i) != 'a' || str.charAt(i) != 'e' || str.charAt(i) != 'i' || str.charAt(i) != 'o' || str.charAt(i) != 'u' || str.charAt(i) != 'y');
            cons++;
          }
        }//end numberOfConsonants
        public int getNumberOfVowels()
        {
          return vowels;
        }
        public int getNumberOfConsonants()
        {
          return consonants;
        }
    }//end class

I'm trying to use this test input when I get the error. I will comment the line it is on.

import java.util.Scanner;
import java.lang.String;
public class testVowelsAndConsonants
{
  public static void main(String[] args)
  {
    Scanner keyboard = new Scanner(System.in);
    int choice;
    System.out.println("Please enter a string.");
    String str = keyboard.nextLine();
    choice = menu(keyboard);
    Character.toLowerCase(choice);
    switch(choice)
    {
      case 'a':
        System.out.println(vowelsAndConsonantsClass.getNumberOfVowels());//ERROR ON THIS LINE
        break;
      case 'b':
        break;
      case 'c':
        break;
      case 'd':
        break;
      case 'e':System.exit(0);
    }
  }
  public static int menu(Scanner keyboard)
    {
    int choice;
    System.out.println("Enter a to count the number of vowels in the string.\n"+
                       "Enter b to count the number of consonants in the string.\n"+
                       "Enter c to count both the vowels and consonants in the string.\n"+
                       "Enter d to enter another string.\n"+
                       "Enter e to Exit.\n");
    do
    {
      choice = keyboard.nextInt();
    }
    while(choice<1||choice>6);
    return choice;
  }//End menu
}

Please note that I'm not 100% done with the test data. I'll finish it once I understand the error. Thanks in advance.

Alex
  • 59
  • 11

3 Answers3

1

You can not call non static method / member through the class name (static call). I suggest you instantiate the the class and then call the non static method:

vowelsAndConsonantsClass v = new vowelsAndConsonantsClass(0, 0);
v.numberOfVowels(str);
System.out.println(v.getNumberOfVowels());
karolovbrat
  • 116
  • 4
  • I'm trying to figure out how you'd incorporate those last two statements. Could you elaborate on where they go? – Alex Sep 25 '14 at 03:14
0

You are trying to call a non-static method in a static way on this line:

vowelsAndConsonantsClass.getNumberOfVowels())

Depending on your need, either create an instance of vowelsAndConsonantsClass or mark getNumberOfVowels as static.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • Well making it static gives the following error on the return line: Cannot make a static reference to the non-static field vowels How would you write the instance? – Alex Sep 25 '14 at 02:46
  • @Alex Yes static methods can only access the static fields of the same class. Better create an instance and proceed. – Juned Ahsan Sep 25 '14 at 02:48
  • I suggest learn how class gets loaded first. you will understand this. all static things in class gets loaded first, so suppose your method is static and variable is non static, your method gets loaded first but your non static variable is something about which your method has no idea so it says that non static variables can not be referred from static method – dev2d Sep 25 '14 at 02:50
  • So I put this in case 'a': vowelsAndConsonantsClass str1 = new vowelsAndConsonantsClass(); and now it says my constructor is undefined. I'm newer to this so I'm pretty confused as to where I'm messing up. – Alex Sep 25 '14 at 02:58
0
System.out.println(vowelsAndConsonantsClass.getNumberOfVowels());

you can refer only static methods of a class using class name, the way you are doing it

so as your getNumberOfVowels() in vowelsAndConsonantsClass is not static, you are getting this error

read more about method referances here

dev2d
  • 4,245
  • 3
  • 31
  • 54