Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Help with class, interface, enum

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    24
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Help with class, interface, enum

    I just finished this guessing game and I have completed the first part
    (answerIsCorrect = ask("Is it a Kiwi? ", keyboard);
    and compiled it with no errors and works. Then I did the same with the bottom and it doesnt work. What have I done wrong?

    public class animalquiz
    {
        public static void main(String[] args)
        {
            Scanner keyboard = new Scanner(System.in);
            boolean answerIsCorrect;
     
            System.out.println("Think of an animal.\n");
     
            if(ask("Is it a bird? ", keyboard))
            {
                if(ask("Is it native to Australia? ", keyboard))
                {
                    answerIsCorrect = ask("Is it a kookaburra? ", keyboard);
                }
                else
                {
                    if(ask("Is it a Colourful? ", keyboard))
                    {
                        answerIsCorrect = ask("Is it a Parrot? ", keyboard);
                    }
                    else
                    {
                        if(ask("Does it fly? ", keyboard))
                        {
                            answerIsCorrect = ask("Is it a Crow? ", keyboard);
                        }
                        else
                        {
                            if(ask("Does it live in a cold place? ", keyboard))
                            {
                                answerIsCorrect = ask("Is it a Penguin? ", keyboard);
                            }
                            else
                            {
                                answerIsCorrect = ask("Is it a Kiwi? ", keyboard);
                            }
                        }
                    }
                }
            }
            else
            {
                {
                    if(ask("Does it live in the ocean? ", keyboard))
                    {
                        answerIsCorrect = ask("Is it a fish? ", keyboard);
                    }
                    else
                    {
                        if(ask("Is it Cold blooded? ", keyboard))
                        {
                            answerIsCorrect = ask("Is it a Snake? ", keyboard);
                        }
                        else
                        {
                            if(ask("Does it have a pouch? ", keyboard))
                            {
                                answerIsCorrect = ask("Is it a Kangaroo? ", keyboard);
                            }
                            else
                            {
                                if(ask("Is it Domestic? ", keyboard))
                                {
                                    answerIsCorrect = ask("Is it a Dog? ", keyboard);
                                }
                            }
                        }
                    }
                }
            }
     
                if(answerIsCorrect)
                {
                    System.out.println("Yes! I am invincible!");
                }
                else
                {
                    System.out.println("Oh. Well, thank you for playing.");
                }
            }
        }
     
     
            /**
             * A utility method to ask a yes/no question
             * 
             * @param question the question to ask
             * @param a scanner for user input
             * 
             * @return whether the user answered "yes" (actually, whether the user answered anything starting with Y or y)
             */
            private static boolean ask(String question, Scanner keyboard)
            {
                System.out.print(question);
     
                String answer = keyboard.nextLine().trim();
     
                return answer.charAt(0) == 'Y' || answer.charAt(0) == 'y';
            }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with class, interface, enum

    it doesnt work.
    Please explain what "doesnt work" means.
    Execute the program in a command prompt window, copy the full contents of the window and paste it here.
    Add some comments to the output describing what the problem is and show what the output should be.

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    24
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with class, interface, enum

    I dont know how to do this... Im writing the program with blue j.
    its the line here thats the issue:

    private static(problem here) boolean ask(String question, Scanner keyboard)

    I know for fact what i wrote works, since I was able to test the logic before this error occurred.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with class, interface, enum

    Please copy the full text of the compiler's error message and paste it here.

    I don't know what the problem is you are talking about. Seeing the error message will help.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    24
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with class, interface, enum

    How do I do this? There is no text. I get this though:

    animal quiz.JPG

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with class, interface, enum

    Please copy the full text of the compiler's error message and paste it here.
    Here is a sample of the javac compiler error message:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    I don't know what you need to do with your IDE to get useful error messages.

    The javac command gives good error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    24
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with class, interface, enum

    Im sorry all I could find is this, and it not even close to what you have:

    Class documentation
    <---- javadoc command: ---->
    C:\Program Files\Java\jdk1.7.0_15\bin\javadoc.exe
    -author
    -version
    -nodeprecated
    -package
    -noindex
    -notree
    -nohelp
    -nonavbar
    -source
    1.7
    -classpath
    C:\Program Files (x86)\BlueJ\lib\bluejcore.jar;C:\Program Files (x86)\BlueJ\lib\junit-4.8.2.jar;C:\Users\Krystle\Desktop\BlueJ projects\Animal Quiz
    -d
    C:\Users\Krystle\Desktop\BlueJ projects\Animal Quiz\doc
    -encoding
    UTF-8
    -charset
    UTF-8
    -docletpath
    C:\Program Files (x86)\BlueJ\lib\bjdoclet.jar
    -doclet
    bluej.doclet.doclets.formats.html.HtmlDoclet
    C:\Users\Krystle\Desktop\BlueJ projects\Animal Quiz\animalquiz.java
    <---- end of javadoc command ---->
    Loading source file C:\Users\Krystle\Desktop\BlueJ projects\Animal Quiz\animalquiz.java...
    C:\Users\Krystle\Desktop\BlueJ projects\Animal Quiz\animalquiz.java:99: error: illegal start of expression
    private static boolean ask(String question, Scanner keyboard)
    ^
    C:\Users\Krystle\Desktop\BlueJ projects\Animal Quiz\animalquiz.java:99: error: illegal start of expression
    private static boolean ask(String question, Scanner keyboard)
    ^
    C:\Users\Krystle\Desktop\BlueJ projects\Animal Quiz\animalquiz.java:99: error: ';' expected
    private static boolean ask(String question, Scanner keyboard)
    ^
    C:\Users\Krystle\Desktop\BlueJ projects\Animal Quiz\animalquiz.java:99: error: ')' expected
    private static boolean ask(String question, Scanner keyboard)
    ^
    C:\Users\Krystle\Desktop\BlueJ projects\Animal Quiz\animalquiz.java:99: error: illegal start of expression
    private static boolean ask(String question, Scanner keyboard)
    ^
    C:\Users\Krystle\Desktop\BlueJ projects\Animal Quiz\animalquiz.java:99: error: ';' expected
    private static boolean ask(String question, Scanner keyboard)
    ^
    C:\Users\Krystle\Desktop\BlueJ projects\Animal Quiz\animalquiz.java:99: error: not a statement
    private static boolean ask(String question, Scanner keyboard)
    ^
    C:\Users\Krystle\Desktop\BlueJ projects\Animal Quiz\animalquiz.java:99: error: ';' expected
    private static boolean ask(String question, Scanner keyboard)
    ^
    8 errors

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with class, interface, enum

    Check that the {}s are correctly paired and in the right location.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    dx2731 (March 24th, 2013)

  10. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    24
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with class, interface, enum

    I've tried and I still get the same error, Could you help edit and check please?

    --- Update ---

    I now have this code, still same error:

    import java.util.Scanner;
    /**
     * Write a description of class animalquiz here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class animalquiz
    {
        public static void main(String[] args)
        {
            Scanner keyboard = new Scanner(System.in);
            boolean answerIsCorrect;
     
            System.out.println("Think of an animal.\n");
     
            if(ask("Is it a bird? ", keyboard))
            {
                if(ask("Is it native to Australia? ", keyboard))
                {
                    answerIsCorrect = ask("Is it a kookaburra? ", keyboard);
                }
                else
                {
                    if(ask("Is it a Colourful? ", keyboard))
                    {
                        answerIsCorrect = ask("Is it a Parrot? ", keyboard);
                    }
                    else
                    {
                        if(ask("Does it fly? ", keyboard))
                        {
                            answerIsCorrect = ask("Is it a Crow? ", keyboard);
                        }
                        else
                        {
                            if(ask("Does it live in a cold place? ", keyboard))
                            {
                                answerIsCorrect = ask("Is it a Penguin? ", keyboard);
                            }
                            else
                            {
                                answerIsCorrect = ask("Is it a Kiwi? ", keyboard);
                            }
                        }
                    }
                }
            }
            else
            {
     
                    if(ask("Does it live in the ocean? ", keyboard))
                    {
                        answerIsCorrect = ask("Is it a fish? ", keyboard);
                    }
                    else
                    {
                        if(ask("Is it Cold blooded? ", keyboard))
                        {
                            answerIsCorrect = ask("Is it a Snake? ", keyboard);
                        }
                        else
                        {
                            if(ask("Does it have a pouch? ", keyboard))
                            {
                                answerIsCorrect = ask("Is it a Kangaroo? ", keyboard);
                            }
                            else
                            {
                                if(ask("Is it Domestic? ", keyboard))
                                {
                                    answerIsCorrect = ask("Is it a Dog? ", keyboard);
                                }
                            }
                        }
     
     
     
     
     
            if(answerIsCorrect)
            {
                System.out.println("Yes! I am invincible!");
            }
            else
            {
                System.out.println("Oh. Well, thank you for playing.");
            }
     
     
            /**
             * A utility method to ask a yes/no question
             * 
             * @param question the question to ask
             * @param a scanner for user input
             * 
             * @return whether the user answered "yes" (actually, whether the user answered anything starting with Y or y)
             */
            private static boolean ask(String question, Scanner keyboard)
            {
                System.out.print(question);
     
                String answer = keyboard.nextLine().trim();
     
                return answer.charAt(0) == 'Y' || answer.charAt(0) == 'y';
            }
     
        }
    }

  11. #10
    Junior Member
    Join Date
    Mar 2013
    Posts
    24
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with class, interface, enum

    I fixed my problem and everything works now.

Similar Threads

  1. class, interface, or enum expected errors??
    By zlloyd1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 28th, 2013, 05:45 AM
  2. Error: class, interface, or enum expected.
    By duderski in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2012, 02:26 PM
  3. Class, Interface, or Enum expected
    By SilvioSpeed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2012, 09:34 AM
  4. class,interface, or enum expected?
    By Eimaj in forum What's Wrong With My Code?
    Replies: 21
    Last Post: September 28th, 2012, 03:16 PM
  5. [SOLVED] Error: Class,interface or enum expected
    By FJIW in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 18th, 2011, 03:08 PM