Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Tolks is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default need to create loop to force user to input integer

    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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Let's see that while loop, in the form of an SSCCE.

    db

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Java 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;
    }
    Last edited by Solarsonic; 05-09-2011 at 12:27 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Nice spoonfeed.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by Tolls View Post
    Nice spoonfeed.
    I don't agree. Smacks of GWBasic. I mean, both continue and break in the same loop where neither is really needed? pah!

    db

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    So, not even an educational spoonfeed then?
    I rarely bother reading the code on these things since they're often a bit scrappy...

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    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:
    Java Code:
    for (int i=0;i<10;i++)
    {
      if (i%2==0)
        continue;
      System.out.println(i);
    }
    it would only print the odd numbers between 0 and 9?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Toll View Post
    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:
    Java Code:
    for (int i=0;i<10;i++)
    {
      if (i%2==0)
        continue;
      System.out.println(i);
    }
    it would only print the odd numbers between 0 and 9?
    Yep, the continue statement transfers the flow of control to the next loop iteration; it could've been written as follows:

    Java Code:
    for (int i= 0; i < 10; i++)
       if (i%2 != 0)
          System.out.println(i);
    See? No continue statement is necessary here.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Assuming I've read that code right, yes.

    ETA: Damn, too slow.

  11. #11
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Yeah; it's about as useless as I thought it was, heh. As I said, never used it, and probably never will.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    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.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    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:

    Java Code:
    for (<for-loop-header>)
       if (<condition1>)
          if (<condition2>)
             <large block of statments here>
    ... could've been written as:

    Java Code:
    for (<for-loop-header>) {
       if (!<condition1> || !<condition2>) continue;
       <large block of statments here>
    }
    ... but then again those <conditions> may become to look 'unnatural' ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    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.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    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:
    Java Code:
    for (MyData myData: lotsOfData) {
        if (dealWithThisOne(myData)) {
            processMyData(myData);
        }
    }

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Quote Originally Posted by Solarsonic View Post
    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.
    Crikey.

    Still a spoonfeed, though...

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Solarsonic View Post
    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.
    Between legally (read: syntactically and semantically) correct code and elegant code lies a world of experience. But indeed I never did much with shit, is it a very smelly job?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    Between legally (read: syntactically and semantically) correct code and elegant code lies a world of experience. But indeed I never did much with shit, is it a very smelly job?

    kind regards,

    Jos
    I've got 5 kids...take it from me, it's a smelly job.

  19. #19
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    If I may rewrite the code to flow more naturally (at least to me):
    Java 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!");
      }
    }
    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)

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    I've got 5 kids...take it from me, it's a smelly job.
    Five kids? Well, consider it your own fault then ;-)

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 03-23-2011, 10:28 PM
  2. Replies: 5
    Last Post: 11-27-2010, 03:49 AM
  3. Read user input into integer array
    By varunb in forum New To Java
    Replies: 12
    Last Post: 07-09-2010, 12:50 PM
  4. [SOLVED] User Input - loop
    By new person in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 10:02 PM
  5. loop when there is no user-input
    By becky in forum New To Java
    Replies: 12
    Last Post: 02-02-2009, 10:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •