All things are lawful, but not all things are profitable.
No.Ryan Jammes wrote:. . .I am curious though. Would there be a way to make something like this work for this problem?..
If you change a public constant which is a compile‑time constant, then there is a risk that some code will retain the old value; you can even have different values in different classes in the same application. So a public compile‑time constant should retain the same value for ever.Once a constant, always a constant.
Knute Snortum wrote:Welcome to the Ranch!
What if you stored num1 in a type bigger than an int?
Something you might not know: the minimum and maximum values for an int are stored in the class Integer.
Also, a note on your code: break long lines up so that the longest line is 80 to 100 characters.
Knute Snortum wrote:Welcome to the Ranch!
What if you stored num1 in a type bigger than an int?
Something you might not know: the minimum and maximum values for an int are stored in the class Integer.
Also, a note on your code: break long lines up so that the longest line is 80 to 100 characters.
Carey Brown wrote:You could put a try/catch block around
num1 = user_input.nextInt();
Campbell Ritchie wrote:
No.Ryan Jammes wrote:. . .I am curious though. Would there be a way to make something like this work for this problem?..
The nextInt method returns a number ≥−2147483648 and ≤2147483647. So the if loop you have will only return true under very specialised circumstances caused by inappropriate use of integer literals. Don't write 2147483647; write Integer.MAX_VALUE. You can also use Integer.MIN_VALUE. Look very closely at your code and the values of max value and min value (use the constant values link). So that if statement can never find an incorrect value for any int.
There is an alternative technique which Rob Spoor taught me. This sort of loop is best put into its own method in a utility class:-Line 1 tests forcorrectincorrect input; if what follows is incorrect input, then line 3 prints an error message and line 4 consumes the offending “token”, doing nothing with it. Some people would use System.out rather than System.err. You can swap lines 4 and 3. Line 6 finds the first non‑incorrect token (now there's some good English grammar!) and converts it to an int.
And welcome to the Ranch
Ryan Jammes wrote:
Knute Snortum wrote:What if you stored num1 in a type bigger than an int?
Hi, yeah i could do that but i am trying to learn how to handle the exception/problem, unless you are saying that it is impossible?
All things are lawful, but not all things are profitable.
Campbell Ritchie wrote:As Rob taught me ages ago, there is no need to mess around with Input Mismatch Exceptions. The loop I showed you a couple of days ago will reliably obviate any risk of that exception.
And sorry for delay in replying.
Knute Snortum wrote:
Ryan Jammes wrote:
Knute Snortum wrote:What if you stored num1 in a type bigger than an int?
Hi, yeah i could do that but i am trying to learn how to handle the exception/problem, unless you are saying that it is impossible?
No, it's not impossible. Take a look at Scanner#nextInt(). It throws an InputFormatException in the case you are interested in. Throw a try/catch block around the getInt() call and see what you get. (Test all kinds of input.)
All things are lawful, but not all things are profitable.
Knute Snortum wrote:
What do you think user_input.equals(0); // Where i left off is doing?
Knute Snortum wrote:
You have all of your code in main(). Create methods, get an instance of SimpleCalc in main() and call your driving method. Basically, nothing should be in main but a few lines of code to start things going.
Ryan Jammes wrote:
Knute Snortum wrote:
What do you think user_input.equals(0); // Where i left off is doing?
I was shooting in the dark to see if i could fix it, my thinking at the time was that the problem was that the stored int was still higher than the valid number for int, so i was trying to set everything that i could think of to 0 to see if it might continue on.
Knute Snortum wrote:
You have all of your code in main(). Create methods, get an instance of SimpleCalc in main() and call your driving method. Basically, nothing should be in main but a few lines of code to start things going.
I am currently starting on objects in the book, so i'll do that as soon as i can.
All things are lawful, but not all things are profitable.
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|