• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help creating a combination Lock that consist of three strings.  RSS feed

 
Hamza Salem
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//My only issue is my set position method. In this method I need to use a counter to keep track of user's input. But i am not sure how to do that.
In this class I am trying to create a combination lock that takes three strings. In the tester class the user inserts three strings and both are compared to see if users input matches correct combination. I have no errors only problem is the setPosition method. My output is always false.






 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick pointer on the issue..

in setPosition move the initializing of counter to where the variable is declared. you don't want the variable to be initialized each time the method is called.
move counter++ to the bottom of the method. Then try..
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have corrected your code tags; you can find out how to use them here. I also got rid of some of the unnecessary blank lines, which don't enhance legibility. Lines like this are particularly difficult to read because of their length and lack of spaces between successive tokens:-Your inconsistent indentation is because you are mixing spaces and tabs. Look at our suggestions. Correcting those things will get you better marks and that is what you want

Do you know how to write arrays? If so, you could replace the several ifs in lines 33-43 with arrays and indices. You might pass the three guesses to one method call and get rid of the counter variable, as an alternative.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!