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

Thread: Newbie coder needs help with number guessing game - Exception in thread "main" java.lang.ClassFormatError: Duplicate field name&signature in class file

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Newbie coder needs help with number guessing game - Exception in thread "main" java.lang.ClassFormatError: Duplicate field name&signature in class file

    Hello all,

    I'm a student who just began a course in Java programming and I'm trying to create a program which generates a random number between 0-99 and then asks the user to guess the number. If the guess was close to the correct answer, the program informs the user. I have tried to get the code to compile and run for several hours now (using NetBeans), and I keep getting various error messages like the one in the title of this thread. Here is the code:

     
    /*This is a number guessing game*/
     
    package labfirst;
    import java.util.Random;
    import java.util.Scanner;
     
    public class Guess {
        public static void main(String[] args) {
     
    //Here the program should create a random number generator
     
                Random random = new Random();
     
    //The program asks the user to input a number and generates a random number
     
                Scanner scanner = new Scanner(System.in);
                System.out.print("Guess a number between 0-99: ");
                int ranInt = random.nextInt(100);
                int userInt = -1; 
            }
                while (userInt!=ranInt)
            {       
                    System.out.print("Incorrect! Guess again: ");
                    userInt = scanner.nextInt();
            }
     
    /*The program compares the user's input with the randomly generated number and comments on whether the number is correct, incorrect or close*/
     
                if (userInt==ranInt - 5) 
            {
     
                System.out.print("The number I was thinking of was " + ranInt + " , you were close.");
     
            }   
                else if (userInt==ranInt + 5) 
            {
     
                System.out.print("The number I was thinking of was ") + ranInt + (" , you were close.");
     
            }
               else if (userInt==ranInt) 
            {
     
                System.out.print("Congratulations, the number was ") + ranInt + (" , you win!");
                System.exit(0);
            } 
     
    }

    Please forgive my ignorance, I'm really new at this. I've edited and re-edited the code so many times (searching online for help) that I'm sure there are more errors now than there were to begin with . This is the exact error message:

    Exception in thread "main" java.lang.ClassFormatError: Duplicate field name&signature in class file labfirst/Guess
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java :792)
    at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader .java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader. java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:4 24)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 57)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.java:482)
    Java Result: 1

  2. The Following User Says Thank You to Zodiak For This Useful Post:

    GregBrannon (August 14th, 2013)


  3. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,285
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Newbie coder needs help with number guessing game - Exception in thread "main" java.lang.ClassFormatError: Duplicate field name&signature in class file

    Thank you for an excellently correct first post. You used code tags, posted relevant code, and posted the entire error message. Thank you, thank you, thank you.

    Your main (forgive the pun) problem is a misplaced close brace at line 21 (in my editor) that ends your main() method before its time. Since your line numbers may vary, here's a locator:
                int userInt = -1; 
        } // this close brace should be deleted!!!!
                while (userInt!=ranInt)
            {       
                    System.out.print("Incorrect! Guess again: ");
    That causes all code that follows to be outside a method which is just a mess.

    The next major problem are your print statements:
    System.out.print("The number I was thinking of was ") + ranInt + (" , you were close.");
    The statement has improperly placed parentheses, so should be corrected to:
    System.out.print("The number I was thinking of was " + ranInt + " , you were close.");
    There are two of those to fix.

    After those corrections, I'm sure you'll have other questions, so come back and ask.

  4. The Following User Says Thank You to GregBrannon For This Useful Post:

    Zodiak (August 14th, 2013)

  5. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Newbie coder needs help with number guessing game - Exception in thread "main" java.lang.ClassFormatError: Duplicate field name&signature in class file

    Thank you so much for the quick reply! I went back and edited my code, and in the process, found some other errors as well. I managed to compile and run it, although it seems I still need to work on it as it does not do exactly what I imagined it would. Nonetheless, I consider this a huge step forward... I was at the point of tearing my hair out after hours of fruitless searching and editing. I'll call this a night for now, but I'll come back and add to my post when I have more questions to ask. Thanks, once again.

  6. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,285
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Newbie coder needs help with number guessing game - Exception in thread "main" java.lang.ClassFormatError: Duplicate field name&signature in class file

    Glad to help you make progress and to save your hair.

    See you later.

Similar Threads

  1. Replies: 1
    Last Post: April 7th, 2013, 04:40 PM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 8
    Last Post: August 9th, 2011, 09:25 PM
  4. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  5. Replies: 13
    Last Post: October 13th, 2010, 12:20 PM