Click here to Skip to main content
15,357,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the code I am supposed to write:
Complete the method: public static boolean isVowel(char letter), that returns true if
the supplied parameter is a vowel.
But, when I wrote the code it is giving me a lot of errors.

MyProgram.java:39: error: class, interface, or enum expected
public static boolean isVowel(char letter)
^
MyProgram.java:42: error: class, interface, or enum expected
if(letter == ('a'))
^
MyProgram.java:46: error: class, interface, or enum expected
return false;
^
MyProgram.java:47: error: class, interface, or enum expected
if(letter == ('e'))
^
MyProgram.java:51: error: class, interface, or enum expected
return false;
^
MyProgram.java:52: error: class, interface, or enum expected
if(letter == ('i'))
^
MyProgram.java:56: error: class, interface, or enum expected
return false;
^
MyProgram.java:57: error: class, interface, or enum expected
if(letter == ('o'))
^
MyProgram.java:61: error: class, interface, or enum expected
return false;
^
MyProgram.java:62: error: class, interface, or enum expected
if(letter == ('u'))
^
MyProgram.java:66: error: class, interface, or enum expected
return false;
^
MyProgram.java:67: error: class, interface, or enum expected
return vowel;}}
^
MyProgram.java:67: error: class, interface, or enum expected
return vowel;}}

What I have tried:

public static boolean isVowel(char letter)
{
boolean vowel = false;
if(letter == ('a'))
{
return true;

return false;
if(letter == ('e'))

return true;

return false;
if(letter == ('i'))

return true;

return false;
if(letter == ('o'))

return true;

return false;
if(letter == ('u'))

return true;

return false;
return vowel;}}
Posted
Updated 12-May-21 4:56am
Comments
Richard MacCutchan 12-May-21 4:16am
   
How many more times are you going to post this same error without actually looking at your code to see what you have mistyped?

Quote:
I am getting a lot of errors in this code
MyProgram.java:39: error: class, interface, or enum expected
   public static boolean isVowel(char letter)
                 ^

Nobody can help you on this because the code you didn't listed here sets a context that make that your code is not Java anymore. We can't guess what you did.

Once correctly indented, one can see that this code is meaningless, a complete rewrite is in order. By the way, 'A' is not a vowel ?
Java
public static boolean isVowel(char letter) {
	boolean vowel = false;
	if(letter == ('a')) {
		return true;

		return false;
		if(letter == ('e'))

			return true;

		return false;
		if(letter == ('i'))

			return true;

		return false;
		if(letter == ('o'))

			return true;

		return false;
		if(letter == ('u'))

			return true;

		return false;
		return vowel;
	}
}

Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
   
v2
Comments
CPallini 12-May-21 2:34am
   
5.
Patrice T 12-May-21 2:42am
   
Thank you
Maciej Los 12-May-21 14:40pm
   
5ed!
Patrice T 12-May-21 14:46pm
   
Thank you.
You have two major problems:
  1. In order to obtain an application, your Java program must be complete, that is you have to specify a class and its main method.
  2. The logic of your code is flawed: you should NOT return false on first mismatch; you are allowed to do that only after all the comparison fail.

I'll give you a working example (Please Note: as pointed out by Patrice, such code doesn't handle correctly uppercase vowels)
Java
public class Vowel
{
  public static boolean isVowel(char letter)
  {
    boolean vowel = false;
    if( letter == 'a' )
      return true;
    else if (letter == 'e')
      return true;
    else if (letter == 'i')
      return true;
    else if (letter == 'o')
      return true;
    else if(letter == 'u')
      return true;
    // here you are allowed to return 'false', because no match occcurred
    return false;
  }

  public static void main(String args[])
  {
    char a[] = { 'h', 'e', 'l', 'l', 'o' };

    for (char c : a)
    {
      System.out.printf("%c is vowel:", c);
      if ( isVowel(c))
        System.out.printf(" yes\n");
      else
        System.out.printf(" no\n");
    }
  }
}
   
v2
Comments
Patrice T 12-May-21 2:43am
   
My 5 too.
CPallini 12-May-21 2:44am
   
Thank you!
Richard MacCutchan 12-May-21 4:17am
   
5th repost of a repost of a repost by OP.
CPallini 13-May-21 6:14am
   
:-D
Maciej Los 12-May-21 14:40pm
   
5ed!
CPallini 13-May-21 6:14am
   
Thank you, Maciej!
You are continuously posting the same code with just the next set of errors you come across. If you continue to do this, then members are less likely to want to help you, so when you come across a real problem, and reach out for help, no-one will listen to you (Aesop's Fables - The Boy Who Cried Wolf[^] )

So here is some more general advice ...

1. Only read the first compilation error. Ignore the rest for now
MyProgram.java:39: error: class, interface, or enum expected
public static boolean isVowel(char letter)
^
2. Really read the error - take in everything it is saying - it is trying to help you solve your problem. See that little ^ character - it is pointing to where the compiler first came across the problem. It's right at the beginning of that line of code. This usually means that the problem is just above that line. Basically everything is fine until the compiler sees that line - it's saying, "Hm, I wasn't expecting that!"
In your case, you have probably missed out a closing brace or inserted an unnecessary opening brace somewhere. It seems to be a habit of yours.

3. Work Tidy.
By this I mean - indent your code "properly". What does "properly" mean? You will notice that Solution 1 and Solution 2 use two different styles of indenting.
Java
public static boolean isVowel(char letter) { //Solution 1 - starts with brace at end of line
but
Java
public static boolean isVowel(char letter)
{ // Solution 2 - starts with brace on a new line
It doesn't matter which one you choose (many, many arguments over the years on this one) but whichever one you choose be consistent
That means, if you copy and paste some code you have found elsewhere, the first thing you should do is convert it to the style of indentation that you have chosen. That might mean that you have to change the tab sizes or the number of spaces used - whatever it takes, tidy that code up straight away.
Why? Because it is a lot easier to spot errors. Trust me. A LOT easier. That list of links in Solution 1 is great - get your editor to do the hard work.

Work tidy also means don't leave loads of commented-out code lying around in your program. Again, it's all about making your code easier to read which makes it easier to spot errors.

4. Only read the first compilation error.
Yes, I know I am repeating myself.
Once you have worked out what the fix is for the first one, recompile. Why? Because Nine Times Out of Ten the rest of the errors will simply just "go away". Especially if the error contains the word "expected".
Never, ever, start looking at compilation faults from the bottom up.

So the corollary to this is ... don't be daunted by a huge list of errors. Just start at the top and deal with one at a time, recompiling after each fix to see what is left.

By the way, if you ever use a compiler that gives warnings as well as errors - treat those compile-time warnings with the same respect as errors. It's a lot easier to fix something that appears at compile time than it is to fix it when it becomes a run-time error!

Finally, please, do start trying to help yourself with this. We really do want to help, but if someone persistently demonstrates that they are not listening to us we very quickly just stop even looking at their posts.
   
Comments
Maciej Los 12-May-21 14:40pm
   
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900