Results 1 to 11 of 11
  1. #1
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Ask for User to input ONLY ten times

    I need to know how to set a limit on how many times my program asks the user for input. For example using:
    Java Code:
    		String Name = JOptionPane.showInputDialog("Enter Number");
    Will constantly ask the user for a number, but how do I allow this sequence to only iterate 10 times. Meaning once the user has entered the 10th number then jump to the next step of code?

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Ask for User to input ONLY ten times

    Can you use a loop?

  3. #3
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Ask for User to input ONLY ten times

    Yes. I was trying to use:
    Java Code:
    count = 0;
    maxCount = 10;
    		
    If (count < maxCount)...
    But couldn't quite tackle that. A loop would suffice :)

  4. #4
    Extraho Vinco is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Ask for User to input ONLY ten times

    Java Code:
    		String[] myarray = new String[11];
    		for (int x = 1; x < myarray.length; x++)
    		{
    			myarray[x] = JOptionPane.showInputDialog("Enter Numter" + "# " + x + ":");
    		}
    Like this?

  5. #5
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Ask for User to input ONLY ten times

    EXACTLY what I was needing!!! I REALLY like the fact you added in the + x + so that in the JOption Pane it shows which number the user needs to enter!

    One more question...how can I validate that the number entered is between 0 and 9?

    Something like:
    Java Code:
    x > 0 && x >= 9

  6. #6
    Extraho Vinco is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Ask for User to input ONLY ten times

    Quote Originally Posted by Rahim2312 View Post
    EXACTLY what I was needing!!! I REALLY like the fact you added in the + x + so that in the JOption Pane it shows which number the user needs to enter!

    One more question...how can I validate that the number entered is between 0 and 9?

    Something like:
    Java Code:
    x > 0 && x >= 9
    If conditionals or a switch statement.
    Let me see what i can come up with real quick.

  7. #7
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Ask for User to input ONLY ten times

    @Extraho Vinco Try not to write code as that isn't "helping".

  8. #8
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Ask for User to input ONLY ten times

    Quote Originally Posted by Extraho Vinco View Post
    If conditionals or a switch statement.
    Let me see what i can come up with real quick.
    I wrote this:
    Java Code:
    if myarray[counter] > 0 && myarray[counter] >= 9
    which may/may not work, but was attempting to try a few results on my own to see if I could conquer my beginner java jitters

  9. #9
    Extraho Vinco is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Ask for User to input ONLY ten times

    Ok, you may need to play around with this to get it to work in your implementation but here is an outline.

    Java Code:
    import javax.swing.JOptionPane;
    
    
    public class Driver {
    
    	private static int d;
    	public static void main(String[] args)
    	{
    		String[] myarray = new String[11];
    		for (int x = 1; x < myarray.length; x++)
    		{
    			myarray[x] = JOptionPane.showInputDialog("Enter Numter " + "# " + x + ":");
    			
    			if (isNumeric(myarray[x]) == true && Integer.valueOf(myarray[x]) >= 0 && Integer.valueOf(myarray[x]) <= 9)
    			{
    				//Continue with operation
    			}
    			else 
    			{
    				x--;
    			}
    		}
    		/*for (int x = 1; x<myarray.length; x++) //This was my debug loop
    		{
    			System.out.println(myarray[x]);
    		}*/
    	}
    	public static boolean isNumeric(String str)
    	{
    		try 
    		{
    			setD(Integer.parseInt(str));
    		}
    		catch(NumberFormatException nfe)
    		{
    			return false;
    		}
    		return true;		
    	}
    	public static int getD() {
    		return d;
    	}
    	public static void setD(int d) {
    		Driver.d = d;
    	}
    }

  10. #10
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Ask for User to input ONLY ten times

    Apalogies, I had a typo that was flawing the code! Thank you for the assistance it is now running as needed!
    Last edited by Rahim2312; 05-07-2012 at 04:12 AM.

  11. #11
    Extraho Vinco is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Ask for User to input ONLY ten times

    Apalogies, I had a typo that was flawing the code! Thank you for the assistance it is now running as needed!
    Np! Awesome to hear! :D
    Last edited by Extraho Vinco; 05-07-2012 at 04:15 AM.

Similar Threads

  1. Secondary input bug within times tables game,
    By Martyn in forum New To Java
    Replies: 14
    Last Post: 01-09-2012, 01:56 AM
  2. User input
    By the ole buc in forum New To Java
    Replies: 16
    Last Post: 12-11-2011, 07:08 PM
  3. Replies: 4
    Last Post: 11-10-2011, 03:49 AM
  4. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 01:59 AM
  5. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 04:47 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
  •