12

This question is about a syntax error OP's getting. The problem is that they're writing

while (1) {
}

When Java expects a boolean, not an integer, as a condition for a while loop.

The question has been closed as a duplicate of this other question which will solve a problem the user already has but will notice only after they solve the actual problem they're asking about.

In particular, the linked question is about the fact that the code is outside a method, which causes a different error then the one OP asked about.

OP's code is outside a method, but this is not what they're asking, even if they will encounter that problem as soon as they solve their current one.

Is that a legitimate duplicate?

7
  • 17
    Let's just add this one to the dupe list and call it a day: Passing an int function to a while loop in Java
    – jscs
    Commented Feb 20, 2019 at 19:53
  • 2
    @JoshCaswell: Done.
    – Robert Harvey Mod
    Commented Feb 20, 2019 at 20:07
  • 1
    Since this is my first question on meta, can the downvoters please explain to me why this question deserves downvotes? Commented Feb 20, 2019 at 20:09
  • 1
    @JoshCaswell: That was...not the right dupe to use. There's a better Java-specific one. I've added it in.
    – Makoto
    Commented Feb 20, 2019 at 20:16
  • 5
    Your original question was not clear at all. Remember, we’re not all Java experts! That’s likely why it was downvoted, and definitely why I closed it. After your edits, this is much clearer and better. So, I reopened. I imagine some folks just haven’t had time to reverse their downvotes. Also worth noting that downvotes on Meta are often used to indicate agreement or disagreement with the issue you’re raising. So, some voters may be trying to say this isn’t an issue worth discussing.
    – Cody Gray Mod
    Commented Feb 20, 2019 at 20:26
  • 1
    Thanks, @Makoto! I thought it explained the error pretty well, but I also don't know Java.
    – jscs
    Commented Feb 20, 2019 at 22:03
  • 2
    @FedericoklezCulloca understand that downvotes on meta are a way to express disagreement. It doesn't necessarily mean you did something wrong the way it does on SO. Commented Feb 23, 2019 at 2:10

2 Answers 2

18

In the past it used to be the case that, if a dupe couldn't answer every tenet of a problem, we would have to reopen the question and allow a more specific answer.

Now, if a question is duplicated by two existing answers, we can add that in fairly straightforward.

I stand by my dupe closure here; the first dupe solves the immediate Java-specific problem that the OP is facing, and the second dupe (added later, admittedly) solves the problem that the OP will face later. There's no reason for us to duplicate this information anywhere else on the site.

14
  • 13
    Not only that, but if we didn't close this question as a duplicate, we'd end up having to close it as "too broad". I personally prefer to err on the side of marking as a duplicate when possible, since that is more helpful to everyone.
    – Cody Gray Mod
    Commented Feb 20, 2019 at 22:53
  • 1
    I prefer, since the question is readily available, to just delete this duplicate. It's not an useful signpost for either of those questions due having multiple issues.
    – Braiam
    Commented Feb 21, 2019 at 15:01
  • 1
    @Braiam: I think the system should take care of it. I don't see it as pressing enough to delete before the system has a chance to.
    – Makoto
    Commented Feb 21, 2019 at 15:42
  • 3
    The system will never have a chance since it has an answer.
    – Braiam
    Commented Feb 21, 2019 at 16:13
  • @Braiam: The answer doesn't score above 0, so it falls in the more aggressive timeline for auto deletion. I don't see it likely that anyone's going to give this question enough attention to upvote the answer, but...
    – Makoto
    Commented Feb 21, 2019 at 16:16
  • 1
    I think you need a refresher on the roomba rules: If the question is more than 30 days old, and: has −1 or lower score; has no answers; is not locked. The score on the answer isn't taken into account when deleting by the rules except when it's closed as non-duplicate.
    – Braiam
    Commented Feb 21, 2019 at 16:19
  • Ah, I had missed that entry when looking at the Roomba rules. Irrespective of that, I really don't see this matter as so pressing that it warrants immediate deletion, however I don't have an opposition to seeing it gone.
    – Makoto
    Commented Feb 21, 2019 at 16:21
  • It needs to no immediate deletion, but someone needs to kick the can if we expect it to see it gone at any point of time.
    – Braiam
    Commented Feb 21, 2019 at 16:29
  • 2
    @CodyGray: Wrt. "... close it as "too broad"" - In what universe is that question too broad?! Yes, it's a duplicate, and it should be closed as such - but anyone who thinks that question is "too broad" should stop flagging and look in a mirror. Groundless kneejerk closures are a huge problem on SO - please do not add to that problem. I get that people want to "get rid of the trash" quickly, to keep it from building up and turning the place into a "dump", but throwing the baby out with the bathwater is counter-productive. Commented Feb 21, 2019 at 19:10
  • 6
    @Aleksi Questions that require that an introductory tutorial on a programming language be typed into the answer box are generally considered "too broad" on Stack Overflow. This is not a tutorial site; we exist to answer (indeed, to build a library of answers to) specific programming questions. When there are multiple different problems going on in a single question, reflecting a fundamental lack of knowledge about the language, that is really getting outside of our domain. These quickly become chameleon questions, where the topic shifts as soon as an answer is given, which are problematic.
    – Cody Gray Mod
    Commented Feb 21, 2019 at 19:15
  • 3
    @CodyGray: Sure. But that question doesn't require an introductory tutorial to answer it. The question boils down to "Why does while(1) give me an error?", and I personally struggle to invent any example of a less broad question. That is very much a specific programming question. If you think that's too broad, you're not judging the question on its own merits, but rather judging the user and extrapolating what you think will happen after the question has been answered. Commented Feb 21, 2019 at 19:33
  • 4
    @AleksiTorhamo: Please note that said error was only the second problem with the question. The first one was, obviously, that the code in the question wasn't in a method. It's this slippery slope which I think Cody is referring to; one problem turns into two; two problems turn into three; three problems turn into a help desk session.
    – Makoto
    Commented Feb 21, 2019 at 19:44
  • The case for "Too Broad" here, as explained by Robert Harvey: meta.stackoverflow.com/a/261859
    – jscs
    Commented Feb 21, 2019 at 19:59
  • 1
    @Makoto: Yes, I noticed that (although I'd say the numbering is the other way around, as Java seems to complain about the other one first). There is a second problem in the code given, but that wasn't what the question was about. Based on the code, the asker will face a second error after he fixes the first one, but that's neither here or there - the question that was asked isn't "too broad" by any means. And it is a sad state of affairs when questions are being closed just because of how someone thinks the asker is going to react to the answer. Commented Feb 21, 2019 at 20:13
2

In particular, the linked question is about the fact that the code is outside a method, which causes a different error then the one OP asked about.

That's not true. That is the problem that the Original Poster asked about. I quote:

on while loop it gives unexpected token error.

I admit it's not literally the same error. It's unexpected token instead of identifier required, but that's just that the code has a while loop there where the other had something (input.name()) that was not a reserved word. The solution is the same. Put the code in a method (where a constructor is a special type of method).

Now, it may be that we should have picked a different dupe target. One that said essentially the same thing but about unexpected token. But putting the code in a method is the correct answer to the question that the OP asked.

The part about needing to say while (true) instead of while (1) is the additional problem that they aren't reaching. The code would get a Boolean required error on that, but the compiler hadn't gotten that far. It still didn't understand why there was a while token where it's expecting a field, method, or interior class declaration. If they had fixed this problem, then they still would have had the unexpected token problem. It would have seemingly made no difference.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .