# Ask for User to input ONLY ten times

• 05-07-2012, 03:01 AM
Rahim2312
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:
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?
• 05-07-2012, 03:02 AM
Wnt2bsleepin
Re: Ask for User to input ONLY ten times
Can you use a loop?
• 05-07-2012, 03:06 AM
Rahim2312
Re: Ask for User to input ONLY ten times
Yes. I was trying to use:
Code:

```count = 0; maxCount = 10;                 If (count < maxCount)...```
But couldn't quite tackle that. A loop would suffice :)
• 05-07-2012, 03:27 AM
Extraho Vinco
Re: Ask for User to input ONLY ten times
Code:

```                String[] myarray = new String[11];                 for (int x = 1; x < myarray.length; x++)                 {                         myarray[x] = JOptionPane.showInputDialog("Enter Numter" + "# " + x + ":");                 }```
Like this? :)-:
• 05-07-2012, 03:33 AM
Rahim2312
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:
Code:

`x > 0 && x >= 9`
• 05-07-2012, 03:36 AM
Extraho Vinco
Re: Ask for User to input ONLY ten times
Quote:

Originally Posted by Rahim2312
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:
Code:

`x > 0 && x >= 9`

If conditionals or a switch statement.
Let me see what i can come up with real quick.
• 05-07-2012, 03:42 AM
brynpttrsn
Re: Ask for User to input ONLY ten times
@Extraho Vinco Try not to write code as that isn't "helping".
• 05-07-2012, 03:53 AM
Rahim2312
Re: Ask for User to input ONLY ten times
Quote:

Originally Posted by Extraho Vinco
If conditionals or a switch statement.
Let me see what i can come up with real quick.

I wrote this:
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 :(handshake):
• 05-07-2012, 04:01 AM
Extraho Vinco
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.

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;         } }```
• 05-07-2012, 04:07 AM
Rahim2312
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!
• 05-07-2012, 04:10 AM
Extraho Vinco
Re: Ask for User to input ONLY ten times
Quote:

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