Click here to Skip to main content
15,035,283 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
MyProgram.java:43: error: illegal start of expression
public static boolean isVowel(char letter)
^
MyProgram.java:43: error: illegal start of expression
public static boolean isVowel(char letter)
^
MyProgram.java:43: error: ';' expected
public static boolean isVowel(char letter)
^
MyProgram.java:43: error: '.class' expected
public static boolean isVowel(char letter)
^
MyProgram.java:43: error: ';' expected
public static boolean isVowel(char letter)
^
MyProgram.java:55: error: illegal start of expression
public static int numVowels(String s)
^
MyProgram.java:55: error: illegal start of expression
public static int numVowels(String s)
^
MyProgram.java:55: error: ';' expected
public static int numVowels(String s)
^
MyProgram.java:55: error: ')' expected
public static int numVowels(String s)
^
MyProgram.java:55: error: illegal start of expression
public static int numVowels(String s)
^
MyProgram.java:55: error: ';' expected
public static int numVowels(String s)
^
11 errors

What I have tried:

Java
public class MyProgram
{
    public static void main(String[] args)

    {
        System.out.println("Problem 1: " + isLetterA('a'));
        System.out.println("Problem 2: " + hasTwoA("computer"));
        System.out.println("Problem 3: " + isVowel('e'));
        System.out.println("Problem 4: " + numVowels("computers"));
        System.out.println("Problem 5: " + evenChars("computerScience", 'e', 's'));
        System.out.println("Problem 6: " + twoInARow("book", 'o'));
        System.out.println("Problem 7: " + capitalizeVowels("hello"));
        System.out.println("Problem 8: " + longestWord("hello how are you "));
        System.out.println("Problem 9: " + containsPrefix("abXYabc", 2));
    }//end main
    //1
    public static boolean isLetterA(char letter)
    {
        return letter == 'a';
    }//end 1
   
    //2 - change n do using char array
    public static boolean hasTwoA (String s)
    {
    {
        int twoA = 0;
        boolean hasA = false;
        for (int i = 0; i < s.length(); i++)
        {
            if ((s.charAt(i)) == ('a'))
            {
                twoA++;
                if (twoA == 2)
                {
                    hasA = true;  
                }
            }
        }
        return hasA;
    }//end 2
   
    //3
    public static boolean isVowel(char letter)
    {
     {  {
          if (letter == ('a') || letter == ('e') || letter == ('i') || letter == ('o') || letter == ('u') || letter == ('A') || letter == ('E') || letter == ('I') || letter == ('O') || letter == ('U'))
          {
            return true;
          }
        return false;
        }//end 3
    }
    }
    //4
    public static int numVowels(String s)
    {
      char[] chars = s.toCharArray();
        int numOfVowels = 0;
        for (int i = 0; i < chars.length; i++)
        {
            if (isVowel(chars[i]) == true)
            {
                numOfVowels++;
            }
        }
        return numOfVowels;
    }//end 4
Posted
Updated 11-May-21 7:40am
v2
Comments
CHill60 11-May-21 12:59pm
   
This is the very same problem as your other question Can someone help this is giving me an error, and idk why[^]
And you accepted that solution!

Look at your code:
Java
//2 - change n do using char array
public static boolean hasTwoA (String s)
{
{ // what is this second open brace for?
   
Your braces do not match up. E.g.
Java
public static boolean hasTwoA (String s)
{
{
Why two {? There is no matching } for one of them. Also true of
Java
public static boolean isVowel(char letter)
{
{
   
v2
Clean up all the curly braces. You should never have multiple opening braces right next to each other with no other text between them, and you've done that in multiple places. You also have to clean up mismatched closing braces as well.

Properly format your code. It makes it much easier to debug.
Java
public class MyProgram
{
    public static void main(String[] args)
    {
        System.out.println("Problem 1: " + isLetterA('a'));
        System.out.println("Problem 2: " + hasTwoA("computer"));
        System.out.println("Problem 3: " + isVowel('e'));
        System.out.println("Problem 4: " + numVowels("computers"));
        System.out.println("Problem 5: " + evenChars("computerScience", 'e', 's'));
        System.out.println("Problem 6: " + twoInARow("book", 'o'));
        System.out.println("Problem 7: " + capitalizeVowels("hello"));
        System.out.println("Problem 8: " + longestWord("hello how are you "));
        System.out.println("Problem 9: " + containsPrefix("abXYabc", 2));
    }//end main

    //1
    public static boolean isLetterA(char letter)
    {
        return letter == 'a';
    }//end 1
   
    //2 - change n do using char array
    public static boolean hasTwoA(String s)
    {
        int twoA = 0;
        boolean hasA = false;

        for (int i = 0; i < s.length(); i++)
        {
            if (s.charAt(i) == 'a')
            {
                twoA++;
                if (twoA == 2)
                {
                    hasA = true;  
                }
            }
        }

        return hasA;
    }//end 2
   
    //3
    public static boolean isVowel(char letter)
    {
        if (letter == ('a') || 
            letter == ('e') ||
            letter == ('i') || 
            letter == ('o') || 
            letter == ('u') || 
            letter == ('A') || 
            letter == ('E') || 
            letter == ('I') || 
            letter == ('O') || 
            letter == ('U') )
        {
            return true;
        }

        return false;
    }//end 3

    //4
    public static int numVowels(String s)
    {
        char[] chars = s.toCharArray();
        int numOfVowels = 0;
        for (int i = 0; i < chars.length; i++)
        {
            if (isVowel(chars[i]) == true)
            {
                numOfVowels++;
            }
        }

        return numOfVowels;
    }//end 4
}
   
Java
//2 - change n do using char array
public static boolean hasTwoA (String s)
{
    { // either this '{' should be removed
        int twoA = 0;
        boolean hasA = false;
        for (int i = 0; i < s.length(); i++)
        {
            if ((s.charAt(i)) == ('a'))
            {
                twoA++;
                if (twoA == 2)
                {
                    hasA = true;
                }
            }
        }
        return hasA;
    }//end 2
    // either there is 1 missing here

Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
public class MyProgram
{
	public static void main(String[] args)

	{
		System.out.println("Problem 1: " + isLetterA('a'));
		System.out.println("Problem 2: " + hasTwoA("computer"));
		System.out.println("Problem 3: " + isVowel('e'));
		System.out.println("Problem 4: " + numVowels("computers"));
		System.out.println("Problem 5: " + evenChars("computerScience", 'e', 's'));
		System.out.println("Problem 6: " + twoInARow("book", 'o'));
		System.out.println("Problem 7: " + capitalizeVowels("hello"));
		System.out.println("Problem 8: " + longestWord("hello how are you "));
		System.out.println("Problem 9: " + containsPrefix("abXYabc", 2));
	}//end main
	//1
	public static boolean isLetterA(char letter)
	{
		return letter == 'a';
	}//end 1

	//2 - change n do using char array
	public static boolean hasTwoA (String s)
	{
		{
			int twoA = 0;
			boolean hasA = false;
			for (int i = 0; i < s.length(); i++)
			{
				if ((s.charAt(i)) == ('a'))
				{
					twoA++;
					if (twoA == 2)
					{
						hasA = true;
					}
				}
			}
			return hasA;
		}//end 2

		//3
		public static boolean isVowel(char letter)
		{
			{  {
				if (letter == ('a') || letter == ('e') || letter == ('i') || letter == ('o') || letter == ('u') || letter == ('A') || letter == ('E') || letter == ('I') || letter == ('O') || letter == ('U'))
				{
					return true;
				}
				return false;
			}//end 3
		}
	}
	//4
	public static int numVowels(String s)
	{
		char[] chars = s.toCharArray();
		int numOfVowels = 0;
		for (int i = 0; i < chars.length; i++)
		{
			if (isVowel(chars[i]) == true)
			{
				numOfVowels++;
			}
		}
		return numOfVowels;
	}//end 4

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[^]
   

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