aspose file tools*
The moose likes Beginning Java and the fly likes Infinite loop problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Infinite loop problem." Watch "Infinite loop problem." New topic
Author

Infinite loop problem.

Riley Redd
Greenhorn

Joined: Jul 28, 2013
Posts: 9
Pastebin link to my code

Can someone please tell me:
Why won't the while loop break, even though the expression is false?



(but in order to reach that I know I have set playerTurn = false: )

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

I can't access your link. many folks work behind firewalls that block such sites.

Can you post your code here so everyone can see it?


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

Joined: Jul 28, 2013
Posts: 9
hmm, whenever I put code in on this site people get upset about code length and what not and then I spend 20 minutes reformatting my code, and even if it is 1 character above 80 after that, people still get upset....so I've been trying to find other solutions to posting directly here ^.^'
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Riley Redd wrote:Why won't the while loop break, even though the expression is false?



(but in order to reach that I know I have set playerTurn = false: )


But the expression isn't false. The value of an assignment expression (the one denoted by "=") is the value of its right-hand side, which is also assigned to the variable on the left-hand side. And that value is clearly "true".

Don't confuse assignment (=) with equality comparison (==).
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Riley Redd wrote:hmm, whenever I put code in on this site people get upset about code length and what not and then I spend 20 minutes reformatting my code, and even if it is 1 character above 80 after that, people still get upset....so I've been trying to find other solutions to posting directly here ^.^'

I'm afraid it's just one of the things you have to do.

First: it was me who complained; and that was more because after I'd just spent 5 minutes breaking up your original post and warning you about it, I came back to find that you'd edited it again, making the lines even longer than they were to start with.

Second: Long lines are simply bad practice, just as not using whitespace or using overly short or long names are. You want people to be able to read your code (and your threads) easily, don't you? That fact is that 1 or 2 characters over 80 isn't likely to make much difference, but 20 will.

Third: There are two problems in your
while (playerTurn = true);
line; either of which might be the issue.

The first, as Paul pointed out, is that you're using the assignment operator. The correct way to write an expression involving a boolean is:
while (playerTurn)

The second is that terminating ';' - It's a null statement, so the loop as written won't do anything, which is yet another reason why it might well run forever, even if you got the expression right.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Riley Redd
Greenhorn

Joined: Jul 28, 2013
Posts: 9
*facepalm*
Why is it always the simple things that make mistakes?
thank you!


(as a side note so I don't make a pointless thread.)
Is using pastebin here bad?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Ouch! Two gotchas in one line of code! I didn't even see that second one.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Riley Redd wrote:Is using pastebin here bad?


It's not "bad" as in "evil bad". But when I see "Link to my code" I suspect a massive code dump, so I don't bother to follow such links. And as fred pointed out, sometimes people can't follow those links. (You might be surprised how much time people spend following the Ranch while they are at work.) Code posted right here is better.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Riley Redd wrote:(as a side note so I don't make a pointless thread.)
Is using pastebin here bad?

I believe Fred already answered that one.

But, quite apart from the basic issue of clicking on "iffy" links, the fact is that folks here don't really want to go through your entire program; they want to see a digest of your problem in condensed form. That in itself requires a fair bit of work on your part, so breaking up excessively long lines is likely to only be a minor irritant.

My advice: Read the SSCCE page. It describes the process extremely well. You might also want to look at HowToAskQuestionsOnJavaRanch (←click).

Winston

PS: I notice that the SSCCE link suggests keeping lines to under 62 characters so, in fact, we're pretty lax here.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

I don't mean to pile on here. But think about it this way...

When you are asking people to give up some of their time to help you, a complete stranger, you should want to make it as EASY as possible for them to do so. You want them to think you really care about this question, and are willing do some work yourself.

This may not be fair or rational. But every single thread I read, these things go through my head: 1) How much effort do I have to put in to help, and 2) how much effort have they put in to this themselves. if 1 < 2, odds are good I will help (if i can). if 1 >= 2, odds are I won't.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

fred rosenberger wrote:if 1 < 2, odds are good I will help (if i can). if 1 >= 2, odds are I won't.

So...you help everyone then? What a dashed decent fellow you are, to be sure.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Infinite loop problem.