Click here to Skip to main content
15,031,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
public class MyProgram
{
    public static void main(String[] args)
    {
        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
   
    //5
    public static boolean evenChars(String s, char a, char b)
    {
        char[] chars = s.toCharArray();
        int numOfA = 0;
        int numOfB = 0;
        for (int i = 0; i < chars.length; i++)
        {
            if (s.charAt(i) == a)
            {
                numOfA++;
            }
            if (s.charAt(i) == b)
            {
                numOfB++;
            }
        }    
        return numOfA == numOfB;
    }//end 5
   
    //6
    public static boolean twoInARow(String s, char a)
    {
        char[] chars = s.toCharArray();
        boolean result = false;
        for (int i = 1; i < chars.length; i++)
        {
            if (s.charAt(i) == a)
            {
                if (s.charAt(i - 1) == a)
                {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }//end 6
   
    //7
    public static String capitalizeVowels(String s)
    {
        char[] chars = s.toCharArray();
        String vowels = "";
        for (int i = 0; i < chars.length; i++)
        {
            if (isVowel(chars[i]) == true)
            {
                chars[i] = Character.toUpperCase(chars[i]);
            }
        }
        String answer = new String(chars);
        return answer;
    }//end 7
   
    //8
    public static String longestWord(String s)
    {
        String[] words = s.split(" ");
        int answer = 0;
        for (int i = 1; i < words.length; i++)
        {
            if (words[i].length() > words[answer].length())
            {
                answer = i;
            }
        }
        return words[answer];
    }
   
    //9
    public static boolean containsPrefix(String s, int n)
    {        
        boolean hasPrefix = false;
        String prefix = s.substring(0,n);
        String excludePrefix = s.substring(n);
        if (excludePrefix.indexOf(prefix) > -1)
        {
            hasPrefix = true;
        }
        return hasPrefix;
    }
}


What I have tried:

MyProgram.java:5: error: illegal start of expression
        public static void main(String[] args)
        ^

MyProgram.java:5: error: illegal start of expression
        public static void main(String[] args)
               ^
MyProgram.java:5: error: ';' expected
        public static void main(String[] args)
                     ^
MyProgram.java:5: error: '.class' expected
        public static void main(String[] args)
                                         ^
MyProgram.java:5: error: ';' expected
        public static void main(String[] args)
                                             ^
MyProgram.java:18: error: illegal start of expression
    public static boolean isLetterA(char letter)
    ^
MyProgram.java:18: error: illegal start of expression
    public static boolean isLetterA(char letter)
           ^
MyProgram.java:18: error: ';' expected
    public static boolean isLetterA(char letter)
                 ^
MyProgram.java:18: error: '.class' expected
    public static boolean isLetterA(char letter)
                                         ^
MyProgram.java:18: error: ';' expected
    public static boolean isLetterA(char letter)
                                               ^
MyProgram.java:24: error: illegal start of expression
    public static boolean hasTwoA (String s)
    ^
MyProgram.java:24: error: illegal start of expression
    public static boolean hasTwoA (String s)
           ^
MyProgram.java:24: error: ';' expected
    public static boolean hasTwoA (String s)
                 ^
MyProgram.java:24: error: ')' expected
    public static boolean hasTwoA (String s)
                                         ^
MyProgram.java:24: error: illegal start of expression
    public static boolean hasTwoA (String s)
                                           ^
MyProgram.java:24: error: ';' expected
    public static boolean hasTwoA (String s)
                                            ^
16 errors
Posted
Updated 10-May-21 19:54pm
v3
Comments
Richard MacCutchan 11-May-21 3:10am
   
Did you look to see what line 5 contains?

1 solution

I would start by remove the duplicates
Java
public class MyProgram
{
    public static void main(String[] args) // this line 
    {
        public static void main(String[] args) // and this, are identical
    {
        System.out.println("Problem 1: " + isLetterA('a'));
   
v2
Comments
CPallini 11-May-21 1:53am
   
:-)
5.
Patrice T 11-May-21 2:02am
   
Thank you

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