File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes ArrayList problem. Head First Java, p. 138 - 139 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList problem. Head First Java, p. 138 - 139 " Watch "ArrayList problem. Head First Java, p. 138 - 139 " New topic
Author

ArrayList problem. Head First Java, p. 138 - 139

Otto Hoel
Greenhorn

Joined: Oct 24, 2010
Posts: 7
We're supposed to use the ArrayList to get around a problem with Arrays. This is in the simple version of the game. Just one row of numbers. Enter a number between 0 and 6 and "hit" a dotCom that occupies three of those cells, hit all three and you get a "kill" and game is over.

The problem; if the dotCom is located in cells 2,3,4 and you type in the number 2, you'll get a hit. If you type 2 again and you get another hit. Hit 2 once more and you got a "kill".

The new and improved code using ArrayList was supposed to fix this, however it doesn't for me. The code is correct, it compiles correctly with no errors, but I can still hit the same cell three times and score a "kill". I don't understand the code well enough to see what the problem might be. Here's the code from the three classes involved, I'm using Eclipse:

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Hi Otto and welcome to the JavaRanch.

Please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will also increase the number of people helping you. I will add them for you since you are new so that you can see the difference.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And please post an sscce when posting code. This code doesn't compile due to multiple errors.
Otto Hoel
Greenhorn

Joined: Oct 24, 2010
Posts: 7
Wouter Oet wrote:And please post an sscce when posting code. This code doesn't compile due to multiple errors.


Guess I have a few things to learn. I will do both next time.

Turns out the code I posted isn't the code I've been working on. Eclipse decided to reset my workspace path after a reboot.
So I'm still looking at this trying to figure it out by running the code from the command prompt as the book suggests.

Otto
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36499
    
  16
You appear to have two package names in the same file. At the simple-dot-com stage, you might do well to delete all the package names from your files and work inside one directory, which Java™ calls the "unnamed package".
Martin Jansen
Greenhorn

Joined: Oct 22, 2010
Posts: 9
- In your DotCom class you have a method setLocationCells with a ArrayList<String> argument but in your calling class SimpleDotComGame you give as argument an Int Array.
- In linenumber 10 of class SimpleDotComGame you try to make an instance of class SimpleDotCom but you named it DotCom.
I hope this information helps you to make your program work
Otto Hoel
Greenhorn

Joined: Oct 24, 2010
Posts: 7
Martin Jansen wrote:- In your DotCom class you have a method setLocationCells with a ArrayList<String> argument but in your calling class SimpleDotComGame you give as argument an Int Array.
- In linenumber 10 of class SimpleDotComGame you try to make an instance of class SimpleDotCom but you named it DotCom.
I hope this information helps you to make your program work


After messing with the files, including the downloaded ones from the the book's web site, for about four hours I realized that I wasn't learning Java, I was just wasting time trying to sort out the mess the authors had left. So I just dropped the book, headed over to Barnes and Noble and picked up "Sam's Teach yourself Java in 24 hours" 5th edition. I'm about half way through it and haven't had to ask any questions so far. It is - in my opinion - a much better book. I'm actually spending my time learning Java. At a slower pace, with better explanations, and with clearer code. And it recommends using Netbeans instead of Notepad

Again, this is just my opinion. "Head first Java" may fit others just fine, but to me it's been a great time waster.
(Although I may return to it at a later date since Sam's doesn't even mention ArrayLists.)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

Otto Hoel wrote:It recommends using Netbeans instead of Notepad.

That, to me, is a reason all by itself to NOT recommend the Sam's book.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Otto Hoel
Greenhorn

Joined: Oct 24, 2010
Posts: 7
fred rosenberger wrote:
Otto Hoel wrote:It recommends using Netbeans instead of Notepad.

That, to me, is a reason all by itself to NOT recommend the Sam's book.


Would you mind expanding on why that is?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

when you are first learning to code, there are a lot of details you need to understand. Using IDEs like Netbeans hides all that from you. You end up learning Netbeans, not java.

You will someday come across a weird error and have no idea how to fix it if you don't know how the compiler works, how classpaths work, etc.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36499
    
  16
I have a copy of the SAMS in 21 days book (an old edition), and I don't like it because I think it puts too much code into the main method and you rarely see objects being created.

Head First has like most books, its own particular style, which some may dislike, but it is in my opinion much better than SAMS.
June Ogterop
Greenhorn

Joined: Apr 14, 2011
Posts: 5

Howdy all

I just wanted to say that I am learning Java as a complete newbie to programming. I have been using the Stanford and Harvard (Academic Earth) tutorials and also a couple of books.

I have tried to work through the SimpleDotCom example in (HFJ chapter 5) but have come across a number of problems. This thread has been helpful in solving a couple of problems - thanks.

I do also have the Sams Teach Yourself Java 6 book. I think that HFJ is more like a colleges approach to teaching and the Sams book is like a university in terms of delivery style and level of explanation given on principles. I think HFJ should be even more basic when giving (what is to a newbie like me) a complex example.

I am trying not to freak out at how difficult it appears, I am going to try and move on from this c5 in HFJ and see if I can come back to it once I have developed my knowledge a bit more.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36499
    
  16
June Ogterop wrote:Howdy all . . .
Howdy to you too, and welcome to the Ranch
I am trying not to freak out at how difficult it appears, . . .
I certainly freaked out when I was just starting. It didn't simply appear difficult, it was difficult.

Have you got a link to those Stanford Tutorials, please; that might be useful for everybody else.
June Ogterop
Greenhorn

Joined: Apr 14, 2011
Posts: 5

Campbell Ritchie wrote:
June Ogterop wrote:Howdy all . . .
Howdy to you too, and welcome to the Ranch
I am trying not to freak out at how difficult it appears, . . .
I certainly freaked out when I was just starting. It didn't simply appear difficult, it was difficult.

Have you got a link to those Stanford Tutorials, please; that might be useful for everybody else.


Of course, the website is Academic Earth. Its packed with learning material for so many topics. For programming and computer science I have found these two the most useful, Programming Methodology and intro into CS

I have spent the last year (in my spare time) freaking out on occasion when trying to teach myself Java programming, so after so long I am trying to use a different tactic ;) and try to just keep calm and work through the problem as opposed to thinking I need to start back at the basics or let it take away from the confidence I have built up so far.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36499
    
  16
Thank you for the links.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList problem. Head First Java, p. 138 - 139
 
Similar Threads
return values in methods
import java.io.*; problem
Head First Java - pg 135 - ArrayList and Compile Errors
While working from Head first java, I came across this problem... Please help
HFJ 2nd - Problem with SimpleDotCom