Results 1 to 11 of 11
  1. #1
    rice's Avatar
    rice is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default Looping if statements

    Java Code:
    import java.util.Scanner;
    public class Packages
    
    {
    	public static void main(String[] args)
    	{
    	Scanner keyboard = new Scanner(System.in);
    	GetInt(keyboard, "Year >> ", 2001, 2025);
    	}
    
    	private static int GetInt(Scanner inStream, String prompt, int minValue, int maxValue)
    	{
    		System.out.print(prompt);
    		int value = inStream.nextInt();
    		if (value <= 2000 || value >= 2025)
    		{
    		System.out.println("*** Value must be between 2001 and 2025");
    		}
    		return value;
    	}
    }
    Hey guys, I'm having a noob trouble looping this.
    I want the validation phrase to show up again if the year entered is outside the given range. But this code, completes the operation once it displays the message.

    Thanks in advance
    Last edited by rice; 10-01-2009 at 01:30 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    So you need a while loop around the code that takes the input.

  3. #3
    rice's Avatar
    rice is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    around this?
    Java Code:
    int value = inStream.nextInt();

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Around everything (input and output) that has to happen over and over again until the user "gets it right".

    By the way, the code you posted doesn't compile. The compiler will try and help you by pointing out that since the method is declared to return an int, it must definitely return one. The while loop's job is to make sure that what you are about to return has a good value.

  5. #5
    rice's Avatar
    rice is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    ^ Thank you! I updated the code and it should compile now.
    But what do I put in the while condition?

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

    Default

    Quote Originally Posted by rice View Post
    But what do I put in the while condition?
    Whatever it is you'd like to test to decide whether to continue looping or not...

  7. #7
    rice's Avatar
    rice is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    Would it be possible if you could help me write it?

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

    Default

    Quote Originally Posted by rice View Post
    Would it be possible if you could help me write it?
    Sure. Let's see your best attempt at implementing this first, and we'll work from there.

    edit: If you need help, please have a look here: While Loops
    Last edited by Fubarable; 10-01-2009 at 02:55 AM.

  9. #9
    rice's Avatar
    rice is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Sure. Let's see your best attempt at implementing this first, and we'll work from there.
    Ooh, hehe, I tried it again and figured it out.

    Java Code:
    import java.util.Scanner;
    public class Packages
    
    {
    	public static void main(String[] args)
    	{
    	Scanner keyboard = new Scanner(System.in);
    	GetInt(keyboard, "Year >> ", 2001, 2025);
    	}
    
    	private static int GetInt(Scanner inStream, String prompt, int minValue, int maxValue)
    	{
    		System.out.print(prompt);
    		int value = inStream.nextInt();
    
    		while (value <= 2000 || value >= 2025)
    		{
    		             if (value <= 2000 || value >= 2025)
    		{
    			System.out.println("*** Value must be between 2001 and 2025");
    		}
    		System.out.print(prompt);
    		value = inStream.nextInt();
    		}
    		
    	return value;
    
    	}
    }
    Thank you!
    But did I write this one right though?

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by rice View Post
    But did I write this one right though?
    The method returns an int so you could print the int it returns as part of the main() method. Then run the program lots of times and see what happens. (Don't forget to try any strange input that the user might type.)

    If you get the output you expect, then it's right.

  11. #11
    rice's Avatar
    rice is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

Similar Threads

  1. basic Java looping
    By ace_hl in forum New To Java
    Replies: 16
    Last Post: 09-03-2009, 01:49 AM
  2. how to do conditional looping?
    By chennee72 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-09-2008, 12:38 PM
  3. Looping problem
    By Tanilo in forum New To Java
    Replies: 1
    Last Post: 08-01-2008, 06:34 AM
  4. Looping ArrayList
    By hai789 in forum Web Frameworks
    Replies: 5
    Last Post: 05-07-2008, 03:55 AM
  5. looping a function
    By Username in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 05:37 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
  •