Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Head First Java P148 - DotComBust - compile errors

Ranch Hand
Posts: 226
jQuery Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have what I (as a total beginner) consider an odd problem. I have spent a couple days thinking about, checked out related posts on this forum, and rechecked the typed code; but I can't find the problem. I imagine it will be simple, but I don't get it (at this stage). (I am using Java 6)

There are three classes. DotComBust DotCom GameHelper
DotCom compiles OK
GameHelper compiles OK
DotComBust does not compile - it complains not finding the 'DotCom' symbol.

Below are the classes and error output.

import java.util.*;
public class DotComBust {
private GameHelper helper = new GameHelper();
private ArrayList<DotCom> dotComsList = new ArrayList<DotCom>();
private int numOfGuesses = 0;

private void setUpGame() {
// first make some dot coms and them locations
DotCom one = new DotCom();
DotCom two = new DotCom();
DotCom three = new DotCom();

System.out.println("Your goal is to sink threee dot coms.");
System.out.println("Try to sink them all in the fewest number of guesses");

for(DotCom dotComToSet : dotComsList){
ArrayList<String> newLocation = helper.placeDotCom(3);
} // close loop
} //close set up game Method

private void startPlaying() {
while(!dotComsList.isEmpty()) {
String userGuess = helper.getUserInput("Enter a guess");
} //close while
} //close start playing game Method

private void checkUserGuess(String userGuess) {
String result = "miss";
for(DotCom dotComToTest : dotComsList) {
result = dotComToTest.checkYourself(userGuess);
if(result.equals("hit")) {
if (result.equals("kill")) {


private void finishGame(){
System.out.println("All Dot Coms are dead! Your stock is now worthless.");
if(numOfGuesses <=18){
System.out.println("It only took you " + numOfGuesses + " guesses.");
System.out.println("You got out before your options sank");
} else {
System.out.println("Took you long enough. " + numOfGuesses + " guesses.");
System.out.println("Fish are dancing with your options.");
} //close method

public static void main (String[] args) {
DotComBust game = new DotComBust();


import java.util.*;

public class DotCom {
private ArrayList<String> locationCells;
private String name;

public void setLocationCells(ArrayList<String> loc) {
locationCells = loc;

public void setName(String n) {
name = n;

public String checkYourself(String userInput) {
String result = "miss";
int index = locationCells.indexOf(userInput);
if(index>=0) {

if(locationCells.isEmpty()) {
result ="kill";
System.out.println("Ouch! you sunk " + name + " : ( ");
} else {
result = "hit";
} // clase if
return result;
} // close method
} // close class

import java.util.*;

public class GameHelper {
private static final String alphabet = "abcdefg";
private int gridLength = 7;
private int gridSize = 49;
private int [] grid = new int[gridSize];
private int comCount = 0;

public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BufferedReader is = new BufferedReader(
new InputStreamReader(;
inputLine = is.readLine();
if(inputLine.length()==0) return null;
} catch (IOException e) {
System.out.println("IOException: " + e);
return inputLine.toLowerCase();

public ArrayList<String> placeDotCom(int comSize) {
ArrayList<String> alphaCells = new ArrayList<String>();
String[] alphacoords = new String [comSize];
String temp = null;
int [] coords = new int[comSize];
int attempts = 0;
boolean success = false;
int location = 0;

int incr = 1;
if ((comCount % 2) == 1) {
incr = gridLength;

while (!success & attempts++ <200){
location = (int) (Math.random() * gridSize);
//System.out.print(" try " + location);
int x=0;
success = true;
while(success && x < comSize) {
if (grid[location] == 0) {
coords[x++] = location;
location += incr;
if (location >= gridSize) {
success = false;
if (x>0 && (location % gridLength ==0)) {
success = false;

} else {
//System.out.print(" used " + location);
success = false;

int x = 0;
int row = 0;
int column = 0;
while (x < comSize) {
grid[coords[x]] = 1;
row = (int) (coords[x] / gridLength);
column = coords[x] % gridLength;
temp = String.valueOf(alphabet.charAt(column));
return alphaCells;

Thanks, any help would be much appreciated.

lowercase baba
Posts: 13019
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do you have the GameHelper.class and DotCom.class files? are they in the \java\Game directory?

I cut-n-pasted your code, as is, into three notepad documents, saved them all in the same directory, and it compiled with no problems.

I'm not using 1.6, but i don't see how that would matter...
author and iconoclast
Posts: 24203
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marten,

It's likely that you've got the CLASSPATH environment variable (which the compiler uses to find classes while it's working) set to something which doesn't include the current directory. From now on, instead of "javac" use "javac -cp ." (that's "javac space dash cp space dot). This will tell the compiler to look for any classes it needs in the current directory, and then things should work fine.
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
marten kay,

I am working with the same book, would love to talk with ya, chat whatever, to discuss any problems.

Ive ran into quite a few myself, would be great having someone else to talk with who is doing same stuff as me.

We cna arrange to exchange IM/Messenger info if you like.

Let me know..
marten koomen
Ranch Hand
Posts: 226
jQuery Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fred, Hi Ernest

Thank you both for you help. Both solution worked.

For information
For this particular game I created a new directory and therefore it started playing up.

Ernests "javac -cp ." did work, then I played around with environment variable so that I could use javac again. I set the CLASSPATH through the tabs on 'My Computer', but this did not work. "I needed to clobber it!" using " Set CLASSPATH=..." in the DOS prompt. After I clobbered the new both, everythin work fine again. I did not know what clobbering meant when I started, but now I do.

Again, thank you so much for your help.

My, my, aren't you a big fella. Here, have a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic