• 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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Otto Hoel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please post an sscce when posting code. This code doesn't compile due to multiple errors.
 
Otto Hoel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79928
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- 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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Otto Hoel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79928
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Og
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79928
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 Og
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79928
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the links.
 
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic