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

Thread: Java Newbie..need help in guessing game program

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post

    Post Java Newbie..need help in guessing game program

    Hi. I'm starting to learn java since a few days..but found myself staring at this roadblock soon enough..the program is as following..i'm not able to compile it because it says error,the file name should be the same as the class name..

    Can somebody please correct the errors in the following program and also suggest the correct file name for the same..?

    public class GuessGame{
    Player p1;
    Player p2;
    Player p3;
     
    public void startGame() {
    p1=new Player();
    p2=new Player();
    p3=new Player();
     
    int guessp1=0;
    int guessp2=0;
    int guessp3=0;
     
    boolean p1isRight=false;
    boolean p2isRight=false;
    boolean p3isRight=false;
     
    int targetNumber = (int) (Math.Random() * 10);
    System.out.println("I'm thinking of a number between 0 and 9...");
     
    while(true) {
    System.out.println("Number to guess is " + targetNumber);
     
    p1.guess();
    p2.guess();
    p3.guess();
     
    guessp1 = p1.number;
    System.out.printn("Player one guessed " + guessp1);
     
    guessp2 = p2.number;
    System.out.printn("Player two guessed " + guessp2);
     
    guessp3 = p3.number;
    System.out.printn("Player three guessed " + guessp3);
     
    if(guessp1==targetNumber)
    p1isRight=true;
     
    if(guessp2==targetNumber)
    p2isRight=true;
     
    if(guessp3==targetNumber)
    p3isRight=true;
     
    if(p1isRight || p2isRight || p3isRight) {
    System.out.println("We have a winner!");
    System.out.println("Player one got it right? " +p1isRight);
    System.out.println("Player two got it right? " +p2isRight);
    System.out.println("Player three got it right? " +p3isRight);
    System.out.println("Game's up.");
    break;
    }
    else System.out.println("Players will have to try again.");
    }
    }
    }
    public class Player {
    int number = 0;
    public void guess() {
    number = (int) (Math.random()*10);
    System.out.println("I'm guessing " +number);
    }
    }
     
    public class GameLauncher {
    public static void main (String[] args) {
    GuessGame game = new GuessGame();
    game.startGame();
    }
    }


  2. #2
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Java Newbie..need help in guessing game program

    First off, we do not know anything about your program or what it does. We aren't going to debug your program for you.. BUT we can guide you.
    Saying: "Hey my program has a bunch of errors, go find them and show me exactly how to do it" ... doesn't really seem enticing for most.

    So tell us what errors you're having, what you've tried, and what you're thinking about doing to fix them.

    Also, for the class name.. It must match your .java file EXACTLY. Spacing, spelling, capitalization, etc... must all be the same.

    Bad Example: filename.java -> class File name THIS WON'T WORK. The f is capitalized and there is a space between the two words. Don't do this.

    However..
    Good Example: filename.java -> class filename This WILL work. Everything matches exactly...

    Please comment back on what your issues are. It makes it easier for us, and helps you learn more. You won't learn by having the answers thrown at you every time you need it.
    Simplicity calls for Complexity. Think about it.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java Newbie..need help in guessing game program


  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Java Newbie..need help in guessing game program

    First off,my apologies for the mis-communication.
    So the specific problem I'm facing with the above program is this..
    As you can see,there are 2 public classes..'Player' and 'GameLauncher'. But the game launcher is the class which contains the main method..so now,even though I name the file exactly as that <classname>.java,it still does not compile. Any suggestions?
    I have tried naming the file as 'Player' and 'GameLauncher' both,with exact resemblance..but still without luck.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Java Newbie..need help in guessing game program

    Oh yeah,it gives me that error which says the file name should be same as the public class name's..but since there are 2 public classes..I'm stuck.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java Newbie..need help in guessing game program

    Split up the classes into 2 separate files named that of the class they contain, or make the class access modifiers package private (in other words, remove the public modifier - not recommended).

  7. The Following User Says Thank You to copeg For This Useful Post:

    confupavan (October 16th, 2011)

  8. #7
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Java Newbie..need help in guessing game program

    Here is how it would go,
    Name this, GuessGame.java
    public class GuessGame{
    Player p1;
    Player p2;
    Player p3;
     
    public void startGame() {
    p1=new Player();
    p2=new Player();
    p3=new Player();
     
    int guessp1=0;
    int guessp2=0;
    int guessp3=0;
     
    boolean p1isRight=false;
    boolean p2isRight=false;
    boolean p3isRight=false;
     
    int targetNumber = (int) (Math.Random() * 10);
    System.out.println("I'm thinking of a number between 0 and 9...");
     
    while(true) {
    System.out.println("Number to guess is " + targetNumber);
     
    p1.guess();
    p2.guess();
    p3.guess();
     
    guessp1 = p1.number;
    System.out.printn("Player one guessed " + guessp1);
     
    guessp2 = p2.number;
    System.out.printn("Player two guessed " + guessp2);
     
    guessp3 = p3.number;
    System.out.printn("Player three guessed " + guessp3);
     
    if(guessp1==targetNumber)
    p1isRight=true;
     
    if(guessp2==targetNumber)
    p2isRight=true;
     
    if(guessp3==targetNumber)
    p3isRight=true;
     
    if(p1isRight || p2isRight || p3isRight) {
    System.out.println("We have a winner!");
    System.out.println("Player one got it right? " +p1isRight);
    System.out.println("Player two got it right? " +p2isRight);
    System.out.println("Player three got it right? " +p3isRight);
    System.out.println("Game's up.");
    break;
    }
    else System.out.println("Players will have to try again.");
    }
    }
    }
    Name this, Player.java
    public class Player {
    int number = 0;
    public void guess() {
    number = (int) (Math.random()*10);
    System.out.println("I'm guessing " +number);
    }
    }
    Name this GameLauncher.java
    public class GameLauncher {
    public static void main (String[] args) {
    GuessGame game = new GuessGame();
    game.startGame();
    }
    }
    You can't have more than one class in the same file, it won't work, another suggestion, get and IDE, such as eclipse or netbeans, it helps for people with all skill levels in java, especially those new to java.

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

    confupavan (October 16th, 2011)

  10. #8
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Java Newbie..need help in guessing game program

    Finally ot my code working!! Thank you so much! Thank you all..
    Final solution that got it working..
    Split the file into 3 files according to the classes, renamed them to their respective public classes, voila!works..

    @tyb97..i'm actually referring to the book head first java, and they ask to use notepad b4 using netbeans..so trying their formula out.. thank you.

Similar Threads

  1. JAVA HIGH LOW NUMBER GUESSING GAME
    By Laxman2809 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 12th, 2011, 10:50 AM
  2. Reverse Number guessing game
    By rarman555 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 23rd, 2011, 10:39 PM
  3. Number Guessing Game
    By JayK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 16th, 2011, 09:46 PM
  4. Guessing Number Game Help
    By Shadow20 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2011, 12:41 PM
  5. im in a hurry!!Help with a programm..java game of guessing a word
    By mr_doctor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:17 AM

Tags for this Thread