By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,680 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,680 IT Pros & Developers. It's quick & easy.

problem with compile

P: n/a

Dear java.help:

I just started learning java and was trying to compile some java sourc
from the Javascript World Submission webpage. I know I have loaded m
JDK successfully since I can compile other examples but when I try t
compile the following code, I get several errors.

I typed "javac J1.java(source file name)"

Any feedback is appreciated.

thanks
Banjo

errors:
class Class1 is public, should be declared in a file named Class1.java

cannot resolve symbol
symbol: variable JOptionPane
code:
//************************************** // Name: Guess My Name /
Description:This is another very simple java program that will ask yo
for a guess of what my name is, then it will either loop and ask agai
if you got it wrong, or it will continue to the next step. Very simple
for very beginners. It's very clean and well commented though. // By
ryan kit // // // Inputs:Name guesses... // // Returns:If they got th
guess right or wrong // //Assumes:None // //Side Effects:None //Thi
code is copyrighted and has limited warranties. //Please se
[url]http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.3491/lngWId.2/qx/vb/scripts/ShowCode.htm[/url
//for details. //************************************** impor
javax.swing.*;
public class Class1
{
public static void main(String[] args)
{

//Declare variables
String myFirstName = "John";
String myLastName = "Smith";
String yourFirstNameGuess;
String yourLastNameGuess;

//Declare / define the variables that will count their guesses
int firstNameGuesses = 5;
int lastNameGuesses = 5;

//Introduce game
JOptionPane.showMessageDialog(null, "Welcome to the sUpeR WacK
CrAZy GAme!");
JOptionPane.showMessageDialog(null, "... or not, try to guess m
name:");

//Get a guess for the first name
yourFirstNameGuess = userGuess("Can you guess my first name?"
+ " You have " + (firstNameGuesses)
+ " guesses left.");
//Subtract one from the amount of guesses they have left
firstNameGuesses--;

//Loop so it asks them for another guess until they guess it o
they run out of guesses
while( !yourFirstNameGuess.equals(myFirstName) &
firstNameGuesses > 0 )
{
JOptionPane.showMessageDialog(null, "Not even close!");
yourFirstNameGuess = userGuess("Try again, what's my firs
name?"
+ " You have " + (firstNameGuesses)
+ " guesses left for my first name.");
//Subtract one from the amount of guesses they have left
firstNameGuesses--;
}

//If they got out of the loop because the guesses the righ
name:
if (firstNameGuesses > 0)
{
JOptionPane.showMessageDialog(null, "That's right, my name is
+ myFirstName
+ ". Can you guess my last name now?");

//Else is if they got out of the loop because they ran out o
guesses
}else
{
JOptionPane.showMessageDialog(null, "Man, you really suck a
this game."
+ " I guess i'll just have to let you go ahead and try t
guess my last name." );
}

//Ask them for a guess for my last name
yourLastNameGuess = userGuess("What's my last name? You have "
lastNameGuesses
+ " guesses left.");

//Subtract one form the amount of guesses they have left
lastNameGuesses--;

//Loop so it asks for another guess until they get the name righ
or they run out of guesses
while(!yourLastNameGuess.equals(myLastName) && lastNameGuesses
0)
{
JOptionPane.showMessageDialog(null, "How could you forget m
last name!");
yourFirstNameGuess = userGuess("Try again, what's my las
name?"
+ " You have " + (lastNameGuesses)
+ " guesses left for my last name.");
//Subtact one form the amount of guesses they have left
lastNameGuesses--;
}

//Tells what to do if they guesses both first and last name
right
if (lastNameGuesses > 0 && firstNameGuesses > 0)
{
JOptionPane.showMessageDialog(null, "Nice! You remembered m
last name"
+ ", and my first name!");

//Elseif tells what to do if they got the last name right but th
first name wrong
}else if (lastNameGuesses > 0 && firstNameGuesses <= 0)
{
JOptionPane.showMessageDialog(null, "Not that bad I guess.
You "
+ "remembered my last name, but you forgot my first name!");

//Else tells what ot do if they got the first name right, but
missed the last name
}else
{
JOptionPane.showMessageDialog(null, "You remembered my first
name, but"
+ " you forgot my last name! How could you?");
}

//My little message...
JOptionPane.showMessageDialog(null, "Hello there, my name is Ryan
Kit,\n"
+ "and i'm looking for a larger project to work on. I know that
my java\n"
+ "skillz aren't top-o-the-line, but i'm very experinced with
html, css,\n"
+ "javascript, php, mysql, and visual basic. I'm also currently
studying"
+ " perl, and directx.\n\n"
+ "I'd be willing to work for free as a:\n"
+ " programmer\n"
+ " html coder\n"
+ " advanced html coder\n"
+ " technical writer\n"
+ " quality assurance analyst\n"
+ " user interface tester\n"
+ " user interface designer\n"
+ " server installation/maintanence\n"
+ " website maintenance\n"
+ "\nYou can check out some of the work i've done with visual
basic, "
+ "and some other lame java stuff at:\n"
+ " www.planet-source-code.com.");
System.exit(0);
}

//Method for asking them for a guess, the argument message tells what
the message
//will say, or what it will ask them.
public static String userGuess(String message)
{
String theirGuess;
theirGuess = JOptionPane.showInputDialog(null, message);
return theirGuess;
}

}

--
banjo123
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"banjo123" <ba*************@mail.codecomments.com> wrote in message
news:1103139200.f237ce927b5338871b71d1787cfd95ff@t ng...

Dear java.help:

I just started learning java and was trying to compile some java source
from the Javascript World Submission webpage. I know I have loaded my
JDK successfully since I can compile other examples but when I try to
compile the following code, I get several errors.

I typed "javac J1.java(source file name)" *************
The name of the file needs to be the same as the class if the class is
public
the first line in the error messages tells the true story.
(You tried to compile "J1.java" and the file should be "Class1.java"
************** errors:
class Class1 is public, should be declared in a file named Class1.java

cannot resolve symbol
symbol: variable JOptionPane
code:
//************************************** // Name: Guess My Name //
Description:This is another very simple java program that will ask you
for a guess of what my name is, then it will either loop and ask again
if you got it wrong, or it will continue to the next step. Very simple,
for very beginners. It's very clean and well commented though. // By:
ryan kit // // // Inputs:Name guesses... // // Returns:If they got the
guess right or wrong // //Assumes:None // //Side Effects:None //This
code is copyrighted and has limited warranties. //Please see
http://www.Planet-Source-Code.com/xq...s/ShowCode.htm //for details. //************************************** import
javax.swing.*;
public class Class1
{
public static void main(String[] args)
{

//Declare variables
String myFirstName = "John";
String myLastName = "Smith";
String yourFirstNameGuess;
String yourLastNameGuess;

//Declare / define the variables that will count their guesses
int firstNameGuesses = 5;
int lastNameGuesses = 5;

//Introduce game
JOptionPane.showMessageDialog(null, "Welcome to the sUpeR WacKY
CrAZy GAme!");
JOptionPane.showMessageDialog(null, "... or not, try to guess my
name:");

//Get a guess for the first name
yourFirstNameGuess = userGuess("Can you guess my first name?"
+ " You have " + (firstNameGuesses)
+ " guesses left.");
//Subtract one from the amount of guesses they have left
firstNameGuesses--;

//Loop so it asks them for another guess until they guess it or
they run out of guesses
while( !yourFirstNameGuess.equals(myFirstName) &&
firstNameGuesses > 0 )
{
JOptionPane.showMessageDialog(null, "Not even close!");
yourFirstNameGuess = userGuess("Try again, what's my first
name?"
+ " You have " + (firstNameGuesses)
+ " guesses left for my first name.");
//Subtract one from the amount of guesses they have left
firstNameGuesses--;
}

//If they got out of the loop because the guesses the right
name:
if (firstNameGuesses > 0)
{
JOptionPane.showMessageDialog(null, "That's right, my name is "
+ myFirstName
+ ". Can you guess my last name now?");

//Else is if they got out of the loop because they ran out of
guesses
}else
{
JOptionPane.showMessageDialog(null, "Man, you really suck at
this game."
+ " I guess i'll just have to let you go ahead and try to
guess my last name." );
}

//Ask them for a guess for my last name
yourLastNameGuess = userGuess("What's my last name? You have " +
lastNameGuesses
+ " guesses left.");

//Subtract one form the amount of guesses they have left
lastNameGuesses--;

//Loop so it asks for another guess until they get the name right
or they run out of guesses
while(!yourLastNameGuess.equals(myLastName) && lastNameGuesses >
0)
{
JOptionPane.showMessageDialog(null, "How could you forget my
last name!");
yourFirstNameGuess = userGuess("Try again, what's my last
name?"
+ " You have " + (lastNameGuesses)
+ " guesses left for my last name.");
//Subtact one form the amount of guesses they have left
lastNameGuesses--;
}

//Tells what to do if they guesses both first and last names
right
if (lastNameGuesses > 0 && firstNameGuesses > 0)
{
JOptionPane.showMessageDialog(null, "Nice! You remembered my
last name"
+ ", and my first name!");

//Elseif tells what to do if they got the last name right but the
first name wrong
}else if (lastNameGuesses > 0 && firstNameGuesses <= 0)
{
JOptionPane.showMessageDialog(null, "Not that bad I guess.
You "
+ "remembered my last name, but you forgot my first name!");

//Else tells what ot do if they got the first name right, but
missed the last name
}else
{
JOptionPane.showMessageDialog(null, "You remembered my first
name, but"
+ " you forgot my last name! How could you?");
}

//My little message...
JOptionPane.showMessageDialog(null, "Hello there, my name is Ryan
Kit,\n"
+ "and i'm looking for a larger project to work on. I know that
my java\n"
+ "skillz aren't top-o-the-line, but i'm very experinced with
html, css,\n"
+ "javascript, php, mysql, and visual basic. I'm also currently
studying"
+ " perl, and directx.\n\n"
+ "I'd be willing to work for free as a:\n"
+ " programmer\n"
+ " html coder\n"
+ " advanced html coder\n"
+ " technical writer\n"
+ " quality assurance analyst\n"
+ " user interface tester\n"
+ " user interface designer\n"
+ " server installation/maintanence\n"
+ " website maintenance\n"
+ "\nYou can check out some of the work i've done with visual
basic, "
+ "and some other lame java stuff at:\n"
+ " www.planet-source-code.com.");
System.exit(0);
}

//Method for asking them for a guess, the argument message tells what
the message
//will say, or what it will ask them.
public static String userGuess(String message)
{
String theirGuess;
theirGuess = JOptionPane.showInputDialog(null, message);
return theirGuess;
}

}

--
banjo123
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.807 / Virus Database: 549 - Release Date: 12/7/2004
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.