Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hey, can you guys help me with this hangman applet

 
Rik Irwin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

I need some help with a hangman applet and I figured this would be the place to go. Anyway, its the input I need help with, Im making a hangman game with a friend and Im doing the input while hes doing the other parts.
So we have the name of the films, books and tv programs for the game as follows in this data file which is saved as a notepad file. This is as follows;

book
cold mountain
book
stalingrad
book
behind the scenes at the museum
film
the thomas crown affair
film
psycho
film
the birds
film
mary poppins
film
the sound of music
tv
bbc news
tv
newsnight
tv
channel 4 news
tv
hollyoaks
tv
top gear
tv
not the nine o'clock news
tv
peak practice
book
harry potter and the philosopher's stone
book
catch 22
tv
columbo
book
far from the madding crowd
tv
ever decreasing circles
tv
the bill
book
private papers
book
the mill on the floss
book
jane eyre
tv
goodnight sweetheart
tv
fawlty towers
book
larry's party
tv
coronation street
book
northern lights
film
angela's ashes
tv
eastenders
film
the end of the affair
tv
frost
tv
red dwarf
film
the beach
book
angela's ashes
book
tis
book
alias grace
tv
top of the pops
tv
emerdale
book
cat's eye
tv
tarrant on tv
book
charlotte gray
tv
match of the day
tv
the simpsons
book
beloved
book
bird song
book
wild swans
book
fatherland
book
flaubert's parrot
book
brazzaville beach
tv
casualty
book
enduring love
book
possession
film
titanic
film
rugrats
film
the piano
tv
newsnight
tv
the royle family
tv
southpark
film
mousehunt
film
toy story
book
the hitchiker's guide to the galaxy
tv
brookside
book
the comedians
tv
only fools and horses
tv
neighbours
tv
friends
tv
frasier
book
tom jones
film
the green mile
book
i claudius
film
the sixth sense
film
american beauty
film
the talented mr ripley
film
topsy turvy
book
for whom the bell tolls
book
the house of the spirits
film
double jeopardy
film
stigmata
film
muppets from space
book
a christmas carol
film
sleepy hollow
book
matilda
book
fermats last theorem
tv
buffy the vampire slayer
tv
star trek
film
toy story 2
book
gulliver's travels
book
great expectations
tv
dinnerladies
tv
heartbeat
tv
london's burning
tv
midsomer murders

As you can see each category is identified by the keyword 'book', 'film', or 'tv', following a line containing the corrosponding title. All text is lowercase and it can be assumed that all text in the file is valid. Also, the info is in no particular order and contains an unspecified number of items. The hangman program we have must read this file of information and then store it in appropriate data structures.
The way we designed the program, when the applet is run the user will be presented with an applet screen asking them to select a book(1), a film(2) or a TV programme(3). The user will put in their selection and click the enter button. This input box will be displayed at the bottom of the applet screen with the enter button to the right. The user can put in a selection of 1, 2, or 3 and if any other input is provided a messsage will be displayed saying 'invalid input - select a book(1), a film(2) or a TV programme(3). The game will only proceed when a valid input has been inputted. So when a category is correctly inputted, a title from that category will be selected at random.

Am I right in thinking this is the the input section of the hangman game complete. After this my friends task will be to load the scaffolds and man with the entry of the selected book etc.

Although how do I do my section of this, I really am stuck and not sure where to even begin. We're meeting up tonight for him to look at my code so I need to have it completed real soon. You guys seem experts on here so any help is greatly appreciated.
Im using JCreatorLE to make this program if you need to know that.
So please help me out here, cus I really am stuck with this one.

Thanks in advance,
Rik
 
Joe Ess
Bartender
Posts: 9339
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch. We'd love to help you, but since this is obviously homework, it is vital that you do your own work. There is only one way to learn how to program, and that is by writing programs. If you really don't know where to begin, perhaps you should speak with your instructor. They would be able to give you the proper amount of help without giving away the answers.
Your question is quite vague. You said what your friends' tasks are, but you did not really indicate what your task is. Sounds like you are doing the GUI, right?
 
Rik Irwin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,

My task is the input yeah, right up until before the scaffolds load up from what the user inputs.
 
Joe Ess
Bartender
Posts: 9339
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what have you written so far?
 
Rik Irwin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far I have,

import java.awt.*;
import java.applet.*;

public class Hangman extends Applet {

public void init() {
}

public void paint(Graphics g) {
g.drawString("Welcome to Java!!", 50, 60 );
}
}

import java.awt.*;
import java.applet.*;
import java.io.*;
import java.net.*;

public class HangmanGame extends Applet{

try {
BufferedReader in = new BufferedReader(new FileReader("hangman"));
String str;
while ((str = in.readLine()) != null) {
process(str);
}
in.close();
} catch (IOException e) {
}

Although I still cant get this to compile

With thanks, Rik
 
Joe Ess
Bartender
Posts: 9339
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. What errors does the compiler give you?
 
Rik Irwin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
errors are as follows;

--------------------Configuration: hangman - JDK version 1.5.0_05 <Default> - <Default>--------------------
C:\Documents and Settings\rik\My Documents\hangman\hangman2.java:14: 'class' or 'interface' expected
import java.awt.*;
^
C:\Documents and Settings\rik\My Documents\hangman\hangman2.java:15: 'class' or 'interface' expected
import java.applet.*;
^
C:\Documents and Settings\rik\My Documents\hangman\hangman2.java:16: 'class' or 'interface' expected
import java.io.*;
^
C:\Documents and Settings\rik\My Documents\hangman\hangman2.java:17: 'class' or 'interface' expected
import java.net.*;
^
C:\Documents and Settings\rik\My Documents\hangman\hangman2.java:21: illegal start of type
try {
^
C:\Documents and Settings\rik\My Documents\hangman\hangman2.java:29: <identifier> expected
}
^
6 errors

Process completed.
 
Joe Ess
Bartender
Posts: 9339
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have all that code in the same file? First, a java source file has a particular format. Import statements come first, then class declarations. You can only have one public class per source file as well.
 
Rik Irwin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right well, i think ive cleared that much up to end up with 2 errors, I will create the code in a java file, then completing that, put it into a java applet; the errors are as follows,

--------------------Configuration: hangman - JDK version 1.5.0_05 <Default> - <Default>--------------------
C:\Documents and Settings\rik\My Documents\hangman\hangman2.java:8: illegal start of type
try {
^
C:\Documents and Settings\rik\My Documents\hangman\hangman2.java:16: <identifier> expected
}
^
2 errors


Also, the code I know have is as follows;

import java.awt.*;
import java.applet.*;
import java.io.*;
import java.net.*;

public class HangmanGame extends Applet{

try {
BufferedReader in = new BufferedReader(new FileReader("hangman"));
String str;
while ((str = in.readLine()) != null) {
process(str);
}
in.close();
} catch (IOException e) {
}

For this input, is this the correct code to have first in this program?

With thanks ,

Rik
 
Joe Ess
Bartender
Posts: 9339
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this example from the Java Tutorial. You can see that a class consists of data members and methods which act on those members. You have not declared a method. You just put your executable code in the body of your class.
You should also be aware that applets have a particular life cycle. Look here for an explanation. You will want to exploit the various life cycle methods of Applet in your own class.
You should also be aware that applets have security restrictions, including not being able to read local files.
 
Rik Irwin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ive tried like you said there, but I am really stuck and now I find out this has to be done for tomarrow, I really have no idea where to go from here and now Im starting to panic. Is this a big load of code for this section of the hangman game Ive been assigned to. Im trying to find out more about user input but Im totally lost.
Really any code you can provide me with is much appreciated, cus I really am very very lost.
With thanks,
Rik
 
Joe Ess
Bartender
Posts: 9339
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rik, as a professional programmer I think it would be irresponsible for me to give you code. You would get credit for work you did not do and you would learn nothing. The next assignment comes along and you are back on the Ranch begging for help.
 
Rik Irwin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that, really I do, its just I am lost with this,

first off, look at what I have done so far;

import java.awt.*;
import java.applet.*;
import java.net.*;

public class hangman.java extends Applet
{
// Member variables of applet
TextField wordField;
Button enterButton;

// Default constructor
public hangman() {
}

// Initialization code for applet
public void init()
{

// Create a text field of size 15 using the 'new' keyword
wordField = new TextField(15);

// Create a new button, labelled Enter!
enterButton = new Button("Enter");

// Add text field and button to our applet's GUI
add(wordField);
add(enterButton);

repaint();
}

}


now this brings up two errors;

--------------------Configuration: hangman - JDK version 1.5.0_05 <Default> - <Default>--------------------
C:\Documents and Settings\rik\My Documents\hangman\src\Hangman.java:5: '{' expected
public class hangman.java extends Applet
^
C:\Documents and Settings\rik\My Documents\hangman\src\Hangman.java:32: '}' expected
}
^
2 errors

So how close am I with this to bringing up the screen with the text field and input button when the applet loads, right now all I can get is the welcome to java message upon running hte program.
I used an online tutorial toget this far but I am still confused.

Thanks for all your help so far anyway,

Rik
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class name can't have a dot in it, so your class name "hangman.java" is being rejected. "Hangman" would be a fine name, stored in a file named "Hangman.java". In Java, letter case is significant.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic