Results 1 to 5 of 5
  1. #1
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default How do I make my println repeat itself?

    Hey, I've been making my first from-scratch program and I wonder about an If statement:

    Java Code:
    int bMonth;
    // bMonth is what the user types as his birthday month in the console, from 1-12.
    
    System.out.println("Please enter your birthday month, as a number from 1 to 12:");
    bMonth = info.nextInt();
    
    if (bMonth < 1 || bMonth > 12) {
         System.out.println("A year only has 12 months. Please enter your birthday month again:");
         bMonth = info.nextInt();
    
    } else {
         System.out.println("Now enter the present month of this year:");
         bMonth = info.nextInt();
    }
    Ignoring unrelated flaws of this code, my question is this: How do I make it so that, the next time you type a value > 12 or < 1, it reruns the same if statement so that the same line pops up consistently. Do I need to make a separate method for it and how would that method look like?

    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How do I make my println repeat itself?

    You need to enclose those statements in a while(...) loop so that they keep repeating until the while loop's boolean expression is false. For more on these constructs, please check out The While Loop.

  3. #3
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Re: How do I make my println repeat itself?

    Quote Originally Posted by Fubarable View Post
    You need to enclose those statements in a while(...) loop so that they keep repeating until the while loop's boolean expression is false. For more on these constructs, please check out The While Loop.
    Ok, thanks. I'm familiar with the While loop but was unsure on its behavior. As I understand it now, a while loop just reruns itself constantly until it can get out of it's own circle, right?

    Problem: It still just runs it once:

    Java Code:
    System.out.println("Now, please enter your birthday month (from 1-12):");
    bMonth = info.nextInt();
    		
    while (bMonth > 12 || bMonth <= 0) {
         System.out.println("A year has 12 months. Please type your birthday month again:");
         bMonth = info.nextInt();
    }
    
    System.out.println("This is the next question in the program");
    I know there's something I've not added here, but I'm just not sure what it is. Maybe I need to make a do while loop?
    Last edited by DrMadolite; 08-04-2012 at 07:40 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How do I make my println repeat itself?

    Your loop works OK for me. My code that I used:

    Java Code:
    import java.util.Scanner;
    
    public class Foo001 {
       public static void main(String[] args) {
          Scanner info = new Scanner(System.in);
          System.out.println("Now, please enter your birthday month (from 1-12):");
          int bMonth = info.nextInt();
                   
          while (bMonth > 12 || bMonth <= 0) {
               System.out.println("I'm sorry, but the year has 12 months, not more and definitely not 0. Please type your birthday month again:");
               bMonth = info.nextInt();
          }
           
          System.out.println("This is the next question in the program");
       }
    }
    A do-while loop could work well here as well.

  5. #5
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Re: How do I make my println repeat itself?

    Hmm, strange. I'll play a round with it some more. If it works for you, then I'm thinking that the value stored in bMonth gets screwed up earlier in the program in some way. In either case, thanks for the quick help.

    Cheers.

Similar Threads

  1. How do you make program repeat question?
    By dexter in forum New To Java
    Replies: 3
    Last Post: 02-17-2011, 02:58 AM
  2. can't make ui:repeat work
    By kaghamdi in forum JavaServer Faces (JSF)
    Replies: 6
    Last Post: 12-31-2010, 12:43 PM
  3. can't make ui:repeat work
    By kaghamdi in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 09-26-2010, 08:19 PM
  4. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 09:52 AM
  5. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-19-2008, 12:06 AM

Posting Permissions

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