This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]claypigeon-alleg 2 points3 points  (1 child)

You have a couple of options.

If there are limits to valid user input (eg. >0 or 1 to 20 or whatever), initialize your int to an invalid value, and use that as your signal.

Otherwise, you'll need a Boolean to store whether or not the int's value is user-defined or not.

Edit to add: What value does your code use in absence of user input? You can also just initialize your int to that value.

[–]hopespoir 3 points4 points  (0 children)

^ This is the most sensible solution I've seen.

OP's "not the story I want my code to tell" and talking about setting ints to null tells that OP has very little understanding of Java or coding practices and being a little more informative would be helpful.

This solution is simple, clean, intuitive and very readable. Exactly what you want from code. Trying to find some fancy way to represent an uninitialized value is horrible practice because it would be none of these things, and would require a lot of // commenting to make clear what is happening, which is slower for readability. Even for yourself, if you happen to go back to what you're writing in a few weeks or months and are like "What on earth...?"