|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

10-16-2008, 10:39 AM
|
 |
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
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.
__________________
$GettinGwap$
|
|

10-16-2008, 11:35 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
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
System.out.print(wins + " wins " + losses + " losses " + );
to this
System.out.print(wins + " wins " + losses + " losses ");
|
|

10-16-2008, 12:35 PM
|
 |
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
thanks
thanks that pretty much fixed it. It's amazing how you can overlook such things.
__________________
$GettinGwap$
|
|

10-17-2008, 04:52 AM
|
 |
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
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?
__________________
$GettinGwap$
|
|

10-17-2008, 06:13 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
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.
|
|

10-17-2008, 08:46 AM
|
 |
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
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.
__________________
$GettinGwap$
|
|

10-17-2008, 11:13 AM
|
|
Senior Member
|
|
Join Date: Sep 2008
Posts: 235
|
|
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
|
|

10-17-2008, 04:33 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
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.
|
|

10-17-2008, 05:59 PM
|
|
Senior Member
|
|
Join Date: Aug 2008
Posts: 178
|
|
You have this
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
__________________
check out To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. , 100% made by me. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

10-19-2008, 06:29 PM
|
 |
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
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.
__________________
$GettinGwap$
Last edited by GettinGwap : 10-19-2008 at 06:39 PM.
|
|

10-19-2008, 06:39 PM
|
|
Member
|
|
Join Date: Sep 2008
Location: Stockholm, Sweden
Posts: 54
|
|
|
Use the CODE or PHP tags to get indentation.
|
|

10-19-2008, 07:31 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
You need to post the FULL text of the error messages so we can see what and where the errors are.
for example:
int aInt = 123;
...
int aInt = 45; // here aInt is already defined above
|
|

10-19-2008, 08:15 PM
|
 |
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
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.
__________________
$GettinGwap$
Last edited by GettinGwap : 10-19-2008 at 08:28 PM.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|