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 11 of 11

Thread: Guessing Game Code... something went wrong

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Guessing Game Code... something went wrong

    Something went wrong with my code.

     
    import java.util.Random;
    import java.util.Scanner;
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    public class GuessingGameByIanNeumann 
    {
        private static String msg;
     
        public static void main(String[] args) 
        {
     
            Random any = new Random();
            Scanner input = new Scanner (System.in); 
     
            do 
            {
               int maxNum, maxTries, userGuess;
               boolean win = false;
               String doAgain = "n";
     
             try
             {
     
                maxNum = any.nextInt(100);
                maxTries = 0;
     
                while (win == false)
                {
     
                System.out.print("\n" + "Pick a number between 1 and 10: ");
                userGuess = input.nextInt();
                maxTries++;
     
                    if (userGuess == maxNum)
                    {
     
                        win = true;
     
                    }
     
                    else if (userGuess < maxNum)
                    {
     
                        System.out.println("\n" + "The number you picked is TOO LOW.");
     
                    }
     
                    else if (userGuess > maxNum)
                    {
     
                        System.out.println("\n" + "The number you picked is TOO HIGH.");
     
                    }
     
                }
     
            System.out.println("\n" + "YOU WIN! The number was: " + maxNum + "\n");
     
            System.out.println("It took this many tries to get it right: " + maxTries + "\n");
     
     
            catch
            {
     
        	input.next();
        	System.out.println("Sorry, invalid input- " + msg + " exception try again ");
     
            } 
     
            } while(doAgain.equalsIgnoreCase("y")); 
     
            }  
    }

    Something went wrong and I got all mixed up. Can anyone help fix this for me, please?


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Guessing Game Code... something went wrong

    then what is wrong?

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game Code... something went wrong

    Well, I'm lost.

    I tried to run it and I got an error.

    I looked at my code and... I don't know what went wrong.

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Guessing Game Code... something went wrong

    then paste the error. is it a compilation error? or a runtime error?

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game Code... something went wrong

    Here:

    java.lang.ExceptionInInitializerError
    Caused by: java.lang.RuntimeException: Uncompilable source code - class GuessingGameByIanNeumann is public, should be declared in a file named GuessingGameByIanNeumann.java
    	at GuessingGameByIanNeumann.<clinit>(demoGuessGame.java:9)
    Exception in thread "main" Java Result: 1

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Guessing Game Code... something went wrong

    what is the name of your java program? if you declared a class in java with public access modifier, the class's name should be exactly the same with the file name of your program.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game Code... something went wrong

    It's GuessingGameByIanNeumann

  8. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Guessing Game Code... something went wrong

    okay, next take a look in your catch clause, what exception is it catching? you must provide an exception when catching it.
    next your condition doAgain.equalsIgnoreCase("y") in your loop. actually the doAgain is not recognize outside the loop block since that variable is declared inside the block of loop

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game Code... something went wrong

    Can you show me?

    Can you borrow my code?

    Bold and Italicize some parts that cna be fixed?

  10. #10
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Guessing Game Code... something went wrong

    Quote Originally Posted by LooneyTunerIan View Post
    Can you show me?

    Can you borrow my code?

    Bold and Italicize some parts that cna be fixed?
    import java.util.Random;
    import java.util.Scanner;
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    public class GuessingGameByIanNeumann 
    {
        private static String msg;
     
        public static void main(String[] args) 
        {
     
            Random any = new Random();
            Scanner input = new Scanner (System.in); 
     
            do 
            {
               int maxNum, maxTries, userGuess;
               boolean win = false;
               String doAgain = "n"; // doAgain was declared inside the do block
     
             try
             {
     
                maxNum = any.nextInt(100);
                maxTries = 0;
     
                while (win == false)
                {
     
                System.out.print("\n" + "Pick a number between 1 and 10: ");
                userGuess = input.nextInt();
                maxTries++;
     
                    if (userGuess == maxNum)
                    {
     
                        win = true;
     
                    }
     
                    else if (userGuess < maxNum)
                    {
     
                        System.out.println("\n" + "The number you picked is TOO LOW.");
     
                    }
     
                    else if (userGuess > maxNum)
                    {
     
                        System.out.println("\n" + "The number you picked is TOO HIGH.");
     
                    }
     
                }
     
            System.out.println("\n" + "YOU WIN! The number was: " + maxNum + "\n");
     
            System.out.println("It took this many tries to get it right: " + maxTries + "\n");
     
            // a compilation error
            catch // what is it catching? catch (Exception e) or any appropriate exception
            {
     
        	input.next();
        	System.out.println("Sorry, invalid input- " + msg + " exception try again ");
     
            } 
     
            } while(doAgain.equalsIgnoreCase("y"));  // another compilation error
               // doAgain variable is not accessible outside the block where it declared
            }  
    }

    kindly see this link for simple tutorial of try-catch http://docs.oracle.com/javase/tutori...ons/catch.html

  11. #11
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game Code... something went wrong

    Nevermind, I'm starting this over on another thread.

    This is getting too confusing.

Similar Threads

  1. Code guessing game, need help with arrays
    By Warow in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 17th, 2013, 02:59 AM
  2. Guessing game
    By namenamename in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2013, 06:47 PM
  3. Guessing Game
    By loreneli113 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 11th, 2013, 07:44 PM
  4. Guessing game help
    By Norm in forum Loops & Control Statements
    Replies: 4
    Last Post: March 23rd, 2013, 08:54 AM
  5. guessing game
    By scottey313 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2011, 02:30 PM

Tags for this Thread