How do i create a loop to force user to input an integer value?
I'm trying to use a while loop and store via keyboard.nextInt(), however as soon as a non integer is entered the program quits with an error.
Printable View
How do i create a loop to force user to input an integer value?
I'm trying to use a while loop and store via keyboard.nextInt(), however as soon as a non integer is entered the program quits with an error.
Let's see that while loop, in the form of an SSCCE.
db
Code:Scanner s = new Scanner(System.in);
int number = 0;
while(true) {
try {
System.out.println("please enter a valid number");
number=Integer.parseInt(s.next());
} catch(NumberFormatException nfe) {
continue;
}
System.out.println("Valid number entered!");
break;
}
Nice spoonfeed.
So, not even an educational spoonfeed then?
I rarely bother reading the code on these things since they're often a bit scrappy...
I noticed that quite a lot of people, new themselves to Java or programming (I don't refer to their post count), try to post answers here; the advice is generally quite bad and those threads go berzerk where the OP leaves with some funny, crappy kind of advice.
kind regards,
Jos
I must admit, I've never used (nor seen) a continue-case. And after reading in my Java-bok, I'm still somewhat confused (the description there was extremely fuzzy)... Am I correct in assuming that with the code:
it would only print the odd numbers between 0 and 9?Code:for (int i=0;i<10;i++)
{
if (i%2==0)
continue;
System.out.println(i);
}
Assuming I've read that code right, yes.
ETA: Damn, too slow.
Yeah; it's about as useless as I thought it was, heh. As I said, never used it, and probably never will.
I wouldn't say useless.
It's often used to avoid unecessarily deep nesting, but that is often in a situation where you are bug fixing, and refactoring a loop is more pain than gain. In other words, you've found a situation where you don't need to process item x so, rather than "if not whatever" and then indenting a score of lines of logic (and potentially cocking that up), you'd just stick "if whatever, continue". Lazy, I know, but sometimes you just need to get a fix in.
Yep, a continue statement may reduce the indentation level; have a look; this:
... could've been written as:Code:for (<for-loop-header>)
if (<condition1>)
if (<condition2>)
<large block of statments here>
... but then again those <conditions> may become to look 'unnatural' ;-)Code:for (<for-loop-header>) {
if (!<condition1> || !<condition2>) continue;
<large block of statments here>
}
kind regards,
Jos
My code is completely correct, I added the continue because it makes more sense that way. You guys haven't done shit so shut your negative mouths up.
Oh yes.
I only mention it in terms of bug fixing, largely because I would (when writing from scratch) tend to break the loop down into more method calls. Something I would do in bug fixing if (for whatever reason) I didn't invariably enter lazy-mode. That's usually down to working on someone elses byzantine code, though.
My stuff tends to look like:
Code:for (MyData myData: lotsOfData) {
if (dealWithThisOne(myData)) {
processMyData(myData);
}
}
If I may rewrite the code to flow more naturally (at least to me):
No more break, no more continue, and no while(true) loop (I've always despised those myself, even though they do have their place at times)Code:Scanner s = new Scanner(System.in);
int number = 0;
boolean validNumber=false;
while(!validNumber)
{
try
{
System.out.println("Please enter a valid number:");
number=Integer.parseInt(s.next());
validNumber=true;
System.out.println("Valid number entered!");
}
catch(NumberFormatException nfe)
{
System.out.println("Not a valid number!");
}
}