0

I am trying to make a application and for one part of the application I need to get a input from the user stating how many times there click their mouse in 1 second. I want the input their give to be between 1-10 and any other number given e.g. 0, -1, 11 to provide them with a error and ask them to input a valid number of 1-10. Also if the user types in any character e.g. name, A, Jo or hello, to also provide them with a error and ask them to provide the correct input. Below is what I have but it does not work.

    int OrginalMouseClick;

    String Mouseclick = JOptionPane.showInputDialog("Write down how many times you can click your mouse button in 1 second");
    int Mouseclick2 = Integer.parseInt(Mouseclick);

    while (Mouseclick2 < 1 || Mouseclick2 > 10) {
        String Mouseclick = JOptionPane.showInputDialog("Write down how many times you can click your mouse button in 1 second");   
         if (Mouseclick2 >= 1 || Mouseclick2 <10) {
             OrginalMouseClick = Mouseclick2;
             }
         }

I haven't yet implemented not to accept any characters like name, j, A, hello because I am not sure how I can do this, can someone show me please.

edit: int mouseClick;

    do {
        while (!str.hasNextInt()) {
            String str = JOptionPane.showInputDialog("Write down how many times you can click your mouse button in 1 second");
            str.next(); // this is important!
        }
        String str = JOptionPane.showInputDialog("Write down how many times you can click your mouse button in 1 second");
        mouseclick = Integer.parseInt(str);
    }
    while (mouseclick < 1 || mouseclick > 10);
2
  • Use a do-while loop instead of a while loop and you can void the repeated code. Additionally you don't parse the newly entered string into an integer... Commented Jan 19, 2014 at 11:10
  • Can you give me a example, please. Commented Jan 19, 2014 at 11:13

2 Answers 2

1

An example as requested:

int mouseClick;

do {
    String str = JOptionPane.showInputDialog("Write down how many times you can click your mouse button in 1 second");
    mouseclick = Integer.parseInt(str);
}
while (mouseclick < 1 || mouseclick > 10);
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Tim, it works, thanks you so much, how would I prevent the user from entering any characters e.g. instead of a number if there type in a letter or letters I want the program to ask to them again to write the number of mouse clicks.
Integer.parseInt will throw an exception. Just catch the exception and in the catch set mouseClick to -1. The loop will then repeat.
Can you please show me an example. I am new to java and learning as I go along.
@user3211917 Google "java exceptions". I'm not writing your whole program for you and I've told you all you need to know to work it out.
0

Use do while loop instead of while loop. This lets you run the loop atleast once before it tests the condition, where you can test the input.

String input = null;
do {
    //get the input from user
} while (isInputValid); //check input validity here

Example:

 Scanner input= new Scanner(System.in);
 do {
    System.out.println("Please enter the advertising cost: ");
    advertCost = input.nextDouble();

    } while (advertCost >= 100000 || advertCost <= 900000);

I have added link on how to validate inputs

Hope this helps!

8 Comments

Hi, Octopus can you please give me a example.
How can I prevent the users from typing in characters.
You cannot prevent users from entering characters. but, you can re-ask the question if the input is invalid
That's another question. but, i will help you. Please go through the link in the updated answer on how to validate inputs
You should accept the answers that to let others know that it is answered and closed. Please ask a new question for further queries.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.