Results 1 to 13 of 13
  1. #1
    GettinGwap's Avatar
    GettinGwap is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Question Need help with Rock Paper Scissors Game

    Hey I'm new to the forums and to Java. I have been writing a rock, paper, scissors game and almost everything seems to be in order. But I have a line of code that contains an error I cannot catch. The compiler says "illegal start of expression when the game ends and prints the users statistics. Here's the code:

    // Author:
    // Started: October 14, 2008
    // Finished:
    //
    // Game of Rock, Paper, Scissors
    //

    import jpb.*;

    public class RPS
    {
    public static void main(String[ ] args)
    {

    // Instance variables
    int wins = 0;
    int losses = 0;
    int draws = 0;
    boolean repeat = true;

    // Loop so the program can run as many times
    // as the user would like
    while(repeat = true)
    {
    SimpleIO.prompt("Rock, paper, or scissors?");
    String choice = SimpleIO.readline( );

    if(choice.equalsIgnoreCase("rock"))
    {
    System.out.println("You lose");
    losses ++;
    }
    else if(choice.equalsIgnoreCase("paper"))
    {
    System.out.print("It's a draw");
    draws ++;
    }
    else if(choice.equalsIgnoreCase("scissors"))
    {
    System.out.println("You win!");
    wins ++;
    }
    else
    {
    System.out.println("I'm sorry, that's not an answer");
    SimpleIO.prompt("Rock, paper, or scissors?");
    String choice = SimpleIO.readline( );
    }

    // Asks the user if they want to continue
    SimpleIO.prompt("Play again?");
    String decision = SimpleIO.readline( );

    if(decision.equalsIgnoreCase("yes"))
    repeat =true;
    else if (decision.equalsIgnoreCase("no"))
    {
    repeat = false;
    // Prints the user's stats
    System.out.print(wins + " wins " + losses + " losses " + );
    System.out.print(draws + " draws");
    }
    // Displays a message depending on performance
    if (wins > losses)
    {
    System.out.println("Good job, thanks for playing.");
    }
    else
    {
    System.out.println("It wasn't your best day.");
    }

    else if
    {
    SimpleIO.prompt("That is not an option. Play again?");
    String decision = SimpleIO.readline( );
    }

    }
    }
    }
    }

    The error is in the two lines that print the user's stats in the middle of the program. This may seem elementary, but I am very new to Java. Thanks in advance.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    C:\jexp>javac rps.java
    rps.java:51: illegal start of expression
                    System.out.print(wins + " wins " + losses + " losses " + );
                                                                             ^
    rps.java:64: 'else' without 'if'
                else if
                ^
    rps.java:71: class, interface, or enum expected
        }
        ^
    3 errors
    on line 51: change this
    Java Code:
    System.out.print(wins + " wins " + losses + " losses " + );
    to this
    Java Code:
    System.out.print(wins + " wins " + losses + " losses ");

  3. #3
    GettinGwap's Avatar
    GettinGwap is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Thumbs up thanks

    thanks that pretty much fixed it. It's amazing how you can overlook such things.

  4. #4
    GettinGwap's Avatar
    GettinGwap is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default reached end of file while parsing

    the program is almost error free. but the IDE I am running and even DOS highlights my last curly brace and says 'reached end of file while parsing'. I tried adding and taking away curly braces and have gotten varied but unwanted results. Taking one away gives me more errors in dealing with my input statements. Adding one also gives me errors with my input statements. Any suggestions or explanations?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Some IDEs have logic to find the matching brace. Check yours.

    Otherwise, print out the source and take a pencil and connect the pairs of braces until you find the missing one. Work from the inside out.

  6. #6
    GettinGwap's Avatar
    GettinGwap is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default confusing

    Wow when I added the matching braces (it's equal now) there are four errors in the program each dealing with my input statements. On every line that contains "string x = SimpleIO.readline()". The error message is "cannot find symbol" (new line) "symbol : method readline()" (new line)
    "location: class jpb.SimpleIO"

    Is the compiler telling me that SimpleIO isn't in the package I used? What about the readline() method? It wasn't saying that before I evened the curly braces.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default

    As SimpleIO isn't a class in the standard JDK, you need to ask wherever you got that class from.

    About mismatching braces: learn to indent your code correctly and it'll never happen again. Well, almost never.
    Code Conventions for the Java(TM) Programming Language: Contents

    luck, db

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    IF the missing class (SimpleIO) is in a jar file, you need to add that jar file to the classpath when you compile and execute your program so that the javac and java programs can find it.

  9. #9
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    You have this
    Java Code:
    else 
    {
    System.out.println("It wasn't your best day.");
    }
    
    else if 
    {
    and a bracket mismatch somewhere.

    And if you're sure it is where it should be, it probably is readLine(), with a capital L
    I die a little on the inside...
    Every time I get shot.

  10. #10
    GettinGwap's Avatar
    GettinGwap is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    My code was indented, but when i pasted it, it wasn't shown that way. Yes capitalizing the readLine worked, but now it is saying that my string variable that is the user's decision, is already defined in main. I don't understand how I can get 1 error after another. It's like Java is never satisfied. Getting kind of frustrating.
    Last edited by GettinGwap; 10-19-2008 at 04:39 PM.

  11. #11
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    Use the CODE or PHP tags to get indentation.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    You need to post the FULL text of the error messages so we can see what and where the errors are.
    is already defined
    for example:
    int aInt = 123;
    ...
    int aInt = 45; // here aInt is already defined above

  13. #13
    GettinGwap's Avatar
    GettinGwap is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Red face

    I finally got it to work. Thank you for sticking with me the enitre time. That was my first program that I attempted on my own outside of schoolwork. Again thanks. Now I want to put it on my phone. It is an LG Fusic. I googled it but I found most people were concerned with ring tones.
    Last edited by GettinGwap; 10-19-2008 at 06:28 PM.

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. game
    By amith in forum AWT / Swing
    Replies: 0
    Last Post: 05-19-2008, 05:16 PM
  3. Paper,Scissor,Rock If then Statements
    By Alberto in forum New To Java
    Replies: 2
    Last Post: 02-11-2008, 11:18 PM
  4. urgent help needed - paper submission.
    By dirtycash in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 11:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •