• Post Reply Bookmark Topic Watch Topic
  • New Topic

Looking for help.  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been stuck trying to figure out whats going wrong for a few hours. Program isnt giving me desired output and I cant tell what I'm doing wrong.

What it should be doing:
Accept a starting point from the user. Then try to complete the knights tour. Mark each stop on the board and then the piece is not allowed to use that spot again. An attempt to move off the 8x8 grid isnt allowed.
I have to use the board class given to me.

What its doing. Accepts the user input and some times it'll do a move or two, but some times it will not compile.



 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you have to get some terms straight. Your statement that your program will do a move or two but doesn't "compile" sometimes is inaccurate. A program must first compile successfully before you can run it. No exceptions. So you probably meant to say that your program fails with an exception sometimes.

I can see at least a couple of problems with your code. First, you have a variable, step, that you initialize to false but never set to true. Yet, your loop condition includes a check on step. When it comes to checking a Boolean value, don't compare the variable to true or false. This is not only redundant, it is also error prone because it's easy to miss a character and write step = false instead of step == false. It's redundant because the variable itself is already sufficient as a Boolean expression. Comparing it to true or false is like asking, "Did you make a step, yes or no?" The "yes or no" part of the question is redundant. Using a variable name that gives more of a sense of yes/no will help you avoid things like this. For example, if you use stepTaken instead, your code would read more naturally:

This reads as "while not stepTaken and ..."

The more serious problem you have in that code is that it's possible to step out of bounds of the board. If you try to do that, you'll get an IndexOutOfBoundsException. You need to perform some checks to ensure that your indices are valid before you try to use them on your board.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, your program is difficult to read because you haven't aligned everything properly. Indent your code properly so that you can clearly tell where each block of statements (if, while-loop, for-loop, method, class, etc.) begins and ends. Imagine your mailbox being in front of your neighbor's house three houses away from yours. That would be confusing for the mailman delivering your mail, right? You're liable to get your mail misdirected. Improperly indented code is like that to people reading your code. They can't easily tell which statements logically go with each other.

Other languages like Python actually require you to indent your code properly in order for the program to behave correctly. Java, however, doesn't care about indentation. Indentation is for the human reader's benefit. So, indent your programs properly for the benefit of its human readers, including yourself.

Refer to the following for guidelines:
http://www.javaranch.com/styleLong.jsp
http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-136091.html

If you are using an IDE like Eclipse, you can have it format your code for you. Just press Ctrl+Shift+F or Cmd+Shift+F (on Mac).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!