Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
MyProgram.java:34: error: class, interface, or enum expected

       public static boolean isVowel(char letter) {
                     ^
MyProgram.java:35: error: class, interface, or enum expected
return letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' ||letter == 'u';}
                                                                                        ^
MyProgram.java:41: error: class, interface, or enum expected
    public static String longestWord(String s) {
                  ^
MyProgram.java:44: error: class, interface, or enum expected
    String answer = words[0];
    ^
MyProgram.java:45: error: class, interface, or enum expected
    for (int i = 1; i < words.length; i++) 
    ^
MyProgram.java:45: error: class, interface, or enum expected
    for (int i = 1; i < words.length; i++) 
                    ^
MyProgram.java:45: error: class, interface, or enum expected
    for (int i = 1; i < words.length; i++) 
                                      ^
MyProgram.java:49: error: class, interface, or enum expected
   }
   ^
MyProgram.java:51: error: class, interface, or enum expected
   public static void question()
                 ^
MyProgram.java:55: error: class, interface, or enum expected
        System.out.println("Which of the following will declare and intialize a variable in Java?");
        ^
MyProgram.java:56: error: class, interface, or enum expected
        System.out.println("1. int 1a=4;");
        ^
MyProgram.java:57: error: class, interface, or enum expected
        System.out.println("2. double circle-area=5.78;");
        ^
MyProgram.java:58: error: class, interface, or enum expected
        System.out.println("3. string s=hello!;");
        ^
MyProgram.java:59: error: class, interface, or enum expected
        System.out.println("4. boolean whoaNow = true;");
        ^
MyProgram.java:60: error: class, interface, or enum expected
        System.out.print("What is the correct answer? ");
        ^
MyProgram.java:61: error: class, interface, or enum expected
        int answer = console.nextInt();
        ^
MyProgram.java:62: error: class, interface, or enum expected
        console.nextLine();
        ^
MyProgram.java:63: error: class, interface, or enum expected
        switch (userInt)
        ^
MyProgram.java:66: error: class, interface, or enum expected
        break;
        ^
MyProgram.java:67: error: class, interface, or enum expected
        case 2:System.out.println("Incorrect");
        ^
MyProgram.java:68: error: class, interface, or enum expected
        break;
        ^
MyProgram.java:69: error: class, interface, or enum expected
        case 3:System.out.println("Incorrect");
        ^
MyProgram.java:70: error: class, interface, or enum expected
        break;
        ^
MyProgram.java:71: error: class, interface, or enum expected
        case 4:System.out.println("Correct");
        ^
MyProgram.java:72: error: class, interface, or enum expected
        break;
        ^
MyProgram.java:73: error: class, interface, or enum expected
        }
        ^
MyProgram.java:76: error: class, interface, or enum expected
   public static int daysInMonth(int month, int year)
                 ^
MyProgram.java:80: error: class, interface, or enum expected
        if ((month < 1 || month > 12) && (year < 1 || year > 9999))
        ^
MyProgram.java:83: error: class, interface, or enum expected
        }
        ^
MyProgram.java:90: error: class, interface, or enum expected
            break;
            ^
MyProgram.java:91: error: class, interface, or enum expected
            case 2:
            ^
MyProgram.java:94: error: class, interface, or enum expected
            break;
            ^
MyProgram.java:95: error: class, interface, or enum expected
            } 
            ^
MyProgram.java:99: error: class, interface, or enum expected
            break;
            ^
MyProgram.java:100: error: class, interface, or enum expected
        case 4:
        ^
MyProgram.java:102: error: class, interface, or enum expected
            break;
            ^
MyProgram.java:103: error: class, interface, or enum expected
        case 5:
        ^
MyProgram.java:105: error: class, interface, or enum expected
            break;
            ^
MyProgram.java:106: error: class, interface, or enum expected
        case 6:
        ^
MyProgram.java:108: error: class, interface, or enum expected
            break;
            ^
MyProgram.java:109: error: class, interface, or enum expected
        case 7:
        ^
MyProgram.java:111: error: class, interface, or enum expected
            break;
            ^
MyProgram.java:112: error: class, interface, or enum expected
        case 8:
        ^
MyProgram.java:114: error: class, interface, or enum expected
            break;
            ^
MyProgram.java:115: error: class, interface, or enum expected
        case 9:
        ^
MyProgram.java:117: error: class, interface, or enum expected
            break;
            ^
MyProgram.java:118: error: class, interface, or enum expected
        case 10:
        ^
MyProgram.java:120: error: class, interface, or enum expected
            break;
            ^
MyProgram.java:121: error: class, interface, or enum expected
        {case 11:
        ^
MyProgram.java:122: error: class, interface, or enum expected
            days = 30;}
                      ^

MyProgram.java:124: error: class, interface, or enum expected
        case 12:
        ^
MyProgram.java:126: error: class, interface, or enum expected
            break;
            ^
MyProgram.java:131: error: class, interface, or enum expected
   public static boolean leapYear(int year)


What I have tried:

Java
import java.util.*;
public class MyProgram
{
   public static void main(String[]args)
   {
       System.out.println("Problem 1 = " + isLetterA('a'));
       question();
       System.out.println(daysInMonth(2,1970));
   }//end main
   
   //problem1
   public static boolean isLetterA(char letter)
   {
       return letter == 'a';
   }//endproblem1
   
   //problem2
   public static boolean hasTwoA(String s)
   {
        boolean q = false;
       int count = 0;
       for(int i = 0; i < s.length(); i++)
       {
           if(s.charAt(i) == 'a')
                count++;
       }
       if(count >= 2)
            return true;
       return q;}
   }
   //endproblem2
   
   //problem3
       public static boolean isVowel(char letter) {
return letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' ||letter == 'u';}
   
   //endproblem3
   
   
   //problem4
    public static String longestWord(String s) {

    String [] words = s.split(" ");
    String answer = words[0];
    for (int i = 1; i < words.length; i++) 
    if (words[i].length() > answer.length()) {
    answer = words[i];
        //endproblem4
   }
   //prob 5
   public static void question()
   {
    
        Scanner console = new Scanner(System.in);
        System.out.println("Which of the following will declare and intialize a variable in Java?");
        System.out.println("1. int 1a=4;");
        System.out.println("2. double circle-area=5.78;");
        System.out.println("3. string s=hello!;");
        System.out.println("4. boolean whoaNow = true;");
        System.out.print("What is the correct answer? ");
        int answer = console.nextInt();
        console.nextLine();
        switch (userInt)
        {
        case 1:System.out.println("Incorrect");
        break;
        case 2:System.out.println("Incorrect");
        break;
        case 3:System.out.println("Incorrect");
        break;
        case 4:System.out.println("Correct");
        break;
        }
   }//end question
   
   public static int daysInMonth(int month, int year)
   {
       
        boolean lYear = leapYear(year);
        if ((month < 1 || month > 12) && (year < 1 || year > 9999))
        {
                return 29;
        }
        else
        {
                switch(month)
                {
            case 1:
            days = 31;
            break;
            case 2:
            if(year == 1) {
            days = 29;
            break;
            } 

            case 3:
            days = 31;
            break;
        case 4:
            days = 30;
            break;
        case 5:
            days = 31;
            break;
        case 6:
            days = 30;
            break;
        case 7:
            days = 31;
            break;
        case 8:
            days = 31;
            break;
        case 9:
            days = 30;
            break;
        case 10:
            days = 31;
            break;
        {case 11:
            days = 30;}
            break;
        case 12:
            days = 31;
            break;
            
        
   //end daysinMonth
   
   public static boolean leapYear(int year)
    {
        if(year % 4 == 0)
        {
                if (year % 100 == 0)
                    {
                        if (year % 400 == 0)
                            {
                                //
                            }
                        //  
                    }
                //
        }
        //
    }
Posted
Updated 12-May-21 21:21pm
v3
Comments
Dave Kreskowiak 12-May-21 23:21pm    
We keep telling you to clean up your curly braces and code formatting and you keep ignoring that piece of advice.

YOUR BAD CODE FORMATTING AND BRACE USE IS WHAT IS CAUSING THE MAJORITY OF YOUR PROBLEMS.
CHill60 13-May-21 3:39am    
On your previous post I explicitly told you to be consistent in your use of braces and you have been told time and time and time again to tidy up your code.
We're not saying this to make things pretty, we're saying this because it makes the problems you are having blindingly, obviously, instantly solvable.
I seriously hope you don't have any more issues because I for one am going to be completely ignoring any post from you from this point forward

Quote:
I changed the code from my previous post but it is still giving me an error

Because you still have errors in your code.
Advice use online code beautifiers to enforce nice coding style, it allow to highlight some coding mistakes.
Online Java Formatter[^]
Result with your code:
Java
import java.util.*;
public class MyProgram {
    public static void main(String[] args) {
        System.out.println("Problem 1 = " + isLetterA('a'));
        question();
        System.out.println(daysInMonth(2, 1970));
    } //end main

    //problem1
    public static boolean isLetterA(char letter) {
        return letter == 'a';
    } //endproblem1

    //problem2
    public static boolean hasTwoA(String s) {
        boolean q = false;
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'a')
                count++;
        }
        if (count >= 2)
            return true;
        return q;
    }
}
//endproblem2

//problem3
public static boolean isVowel(char letter) {
    return letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u';
}

//endproblem3


//problem4
public static String longestWord(String s) {

        String[] words = s.split(" ");
        String answer = words[0];
        for (int i = 1; i < words.length; i++)
            if (words[i].length() > answer.length()) {
                answer = words[i];
                //endproblem4
            }
        //prob 5
        public static void question() {

            Scanner console = new Scanner(System.in);
            System.out.println("Which of the following will declare and intialize a variable in Java?");
            System.out.println("1. int 1a=4;");
            System.out.println("2. double circle-area=5.78;");
            System.out.println("3. string s=hello!;");
            System.out.println("4. boolean whoaNow = true;");
            System.out.print("What is the correct answer? ");
            int answer = console.nextInt();
            console.nextLine();
            switch (userInt) {
                case 1:
                    System.out.println("Incorrect");
                    break;
                case 2:
                    System.out.println("Incorrect");
                    break;
                case 3:
                    System.out.println("Incorrect");
                    break;
                case 4:
                    System.out.println("Correct");
                    break;
            }
        } //end question

        public static int daysInMonth(int month, int year) {

                boolean lYear = leapYear(year);
                if ((month < 1 || month > 12) && (year < 1 || year > 9999)) {
                    return 29;
                } else {
                    switch (month) {
                        case 1:
                            days = 31;
                            break;
                        case 2:
                            if (year == 1) {
                                days = 29;
                                break;
                            }

                        case 3:
                            days = 31;
                            break;
                        case 4:
                            days = 30;
                            break;
                        case 5:
                            days = 31;
                            break;
                        case 6:
                            days = 30;
                            break;
                        case 7:
                            days = 31;
                            break;
                        case 8:
                            days = 31;
                            break;
                        case 9:
                            days = 30;
                            break;
                        case 10:
                            days = 31;
                            break; {
                                case 11:
                                    days = 30;
                            }
                            break;
                        case 12:
                            days = 31;
                            break;


                            //end daysinMonth

                            public static boolean leapYear(int year) {
                                if (year % 4 == 0) {
                                    if (year % 100 == 0) {
                                        if (year % 400 == 0) {
                                            //
                                        }
                                        //  
                                    }
                                    //
                                }
                                //
                            }

As one can see, IsVowel is out of the class, and other such errors later too..
 
Share this answer
 
Do you ever look at your code?
Java
    return q;} // Why is this closing brace here?
} // especially when this should be the method close
 
Share this answer
 

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