Why is it printing the value as 0?

+1 Hemlata Mahaur · April 2, 2017
When I am trying to print the value of "player.getLengthOfAShip()" it prints the value as expected, but when I try to store the value in a variable and then try to print it out, it print it as 0. Can anyone please tell me why is it printing it out as 0? It's same in case of row and column variable as well. They both also prints 0.
If you need to check  more code just in case I am doing something wrong in the other classes. Here is my github: https://github.com/Hersheyhem/Battleship/tree/master/src/bship/images/forum/upload/2017-04-02/a85880b52d8e85dd78b7eb5d3467c2fa.png/images/forum/upload/2017-04-02/94f694f66b660e4e786208aa7b68aeba.png
Any help is appreciated.

Post a Reply


Oldest  Newest  Rating
0 Nicholas Eason · April 5, 2017
It's been a few days, and the code on GitHub is not the same as the code here (which is fine). Before I look into it, did you figure this out :P?
0 Hemlata Mahaur · April 5, 2017
Yeah, I solved it, but now I have more problems with my code. Plus I edited a lot cuz my code wasn't right and still it is not right.
0 Hemlata Mahaur · April 5, 2017
But I changed the variables to static then it was working. So if you can tell me why is it so, it is appreciated. :)
0 Nicholas Eason · April 5, 2017
Which variables did you make static to fix in (and it which classes)?
0 Dol Lod · April 5, 2017
Have you learned how to use the debugger in whatever IDE you're working in? Setting break points or watching variables change their values means you can determine what is going wrong much faster. I would also recommend making constructors for setting default values when you rely on values previously defined. Scanner input = new Scanner(System.in); would be a perfect example of what to move to inside a constructor.

Lastly, I would also recommend refactoring the names of your class variables to differentiate them from local variables. Personally, I use m_ as a prefix to indicate it's a variable belonging to the class, but it's up to you.
0 Hemlata Mahaur · April 6, 2017
I changed row and column variables to static and they were probably in the ship class. 
0 Hemlata Mahaur · April 6, 2017
Dol Lod, I don't how how to debug the program, but if you can tell me it is appreciated. :)
0 Dol Lod · April 6, 2017
For whatever IDE, you're using (Probably Eclipse or IntelliJ), there's a run tab. In that there is a debug option. If you click next to line numbers, you can set breakpoints which pauses the program when you run it in debug mode allowing you to see variables and when variables change their value. I think Bucky should have covered the debugger in his tutorials, but I may be wrong.

Learning how to use the Debugger is critical if you plan on doing programming. It saves an incredible amount of time.
0 Hemlata Mahaur · April 7, 2017
Dol Lod, thanks!
0 Dol Lod · April 8, 2017
No problem.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator