3

I'm trying to ask for an integer between 4 and 10 from the user. If they answer out of that range it will go into a loop. When the user inputs the number correctly the first time it will not break and continues to else statement. If the user inputs the number correctly during the else statement it will properly break.

How can I get it to break if input is correct the first time around?

**I am very much a beginner at java and do apologize if it is something silly i'm missing here.

public int companySize() {  

    int ansCompany;

    do {
        ansCompany = Integer.parseInt(JOptionPane.showInputDialog
                ("Please input the company size"));

        if ( ansCompany <= 4 && ansCompany <= 10 ) {
                         break;

        } else {
       ansCompany = Integer.parseInt(JOptionPane.showInputDialog
                ("Please enter a valid company size"));

        if ( ansCompany <= 4 && ansCompany <= 10 ) {
                          break;
        } // ends nested if condition
    } //ends else 
          }//ends do
    while ( ansCompany < 4 || ansCompany > 10);
    return ansCompany;
}// ends public int companySize()

I'm calling it from main as follows:

public static void main(String[] args) {

UserInput getResult = new UserInput();
int company_size =  getResult.companySize();

}// ends main
Censored
  • 46
  • 1
  • 6

3 Answers3

2

I am not sure why you need two identical dialogs if the user would write the wrong value the first time, since in the end only one value will be returned (ansCompany).

By setting the do-while statement to the break condition (less than 4 or greater than 10) it will loop until the user inputs the correct number.

public int companySize() {
 int ansCompany;

 do {
   ansCompany = Integer.parseInt(JOptionPane.showInputDialog
      ("Please input the company size"));

 } while (ansCompany < 4 || ansCompany > 10);

 return ansCompany;

}

asiew
  • 493
  • 4
  • 13
  • I can see how superfluous that was. I'm not sure why I figured I needed break statements.. but I can certainly see now why it would run with just do. Thank you – Censored Dec 21 '15 at 11:41
  • I see the second dialog would only be useful if I wanted to tell the user that the input was invalid but not a necessity. – Censored Dec 21 '15 at 11:48
  • 1
    No problem, be careful when using the code without handling the exceptions that would arise if a person would write a non-integer value. With this implementation the program would just crash! – asiew Dec 21 '15 at 11:50
  • Haven't really touched anything with catching exceptions yet but I cannot wait to go back and implement it into this . – Censored Dec 21 '15 at 11:55
  • http://stackoverflow.com/questions/6456219/java-checking-if-parseint-throws-exception – LowLevel Dec 21 '15 at 12:09
1

while ( ansCompany < 4 || ansCompany > 10); is anything below 3 OR higher than 11

You want while ( ansCompany >= 4 && ansCompany <= 10);

Note: The reason you want this choice ^^^^ is because if the input is greater than > or equal = making >= than means 4 and above. Likewise for less than < or equal = making <= than means anything less than 10

The || means OR. This mean the input must be greater than 4 or less than 10. If the answer is 11, It passes the first condition and therefore passes the if statement. Likewise with a 3, it passes the less than 10 condition.

&& mean AND, and therefore must pass the first condition, AND the second condition.

Your if statements also are wrong in this sense;

if ( ansCompany <= 4 && ansCompany <= 10 ) {

should be

if ( ansCompany >= 4 && ansCompany <= 10 ) {

Tomaltach
  • 913
  • 1
  • 11
  • 30
  • If I want the user to enter a number between 4 and 10, wouldn't while ( ansCompany < 4 || ansCompany < 10); warrant a loop if it's entered out of those bounds? Could you explain a little more where I went wrong? I tried your changes and I still can't get it to break properly. – Censored Dec 21 '15 at 11:28
  • Sorry I see where I went wrong with the while and if statements now.. Thank you – Censored Dec 21 '15 at 11:35
1

I would rather use a recursive function:

Version1: In short:

public int companySize() {
    final int result = Integer.parseInt(JOptionPane.showInputDialog("Please input the company size"));
    return (result >= 4 && result <= 10) ? result : companySize();
}

Version 2: But you also can use static constants

/* don't be afraid of shared constants; sometimes they're very "useful idiots" */
/*public*/ static final int MIN = 4;
/*public*/ static final int MAX = 10;
/*public*/ static final String MESSAGE = "Please input the company size";

public int companySize() {
    final int result = Integer.parseInt(JOptionPane.showInputDialog(MESSAGE));
    return (result >= MIN && result <= MAX) ? result : companySize();
}

Version 3: And if you want to show an error message:

/* don't be afraid of shared constants; sometimes they're very "useful idiots" */
/*public*/ static final int MIN = 4;
/*public*/ static final int MAX = 10;
/*public*/ static final String MESSAGE = "Please input the company size";
/*protected*/ static final String ERROR_MESSAGE = "Please input a valid company size";

public int companySize() {
    final int result = Integer.parseInt(JOptionPane.showInputDialog(MESSAGE));
    return (result >= MIN && result <= MAX) ? result : companySize(ERROR_MESSAGE);
}

private int companySize(String errorMessage) {
    final int result = Integer.parseInt(JOptionPane.showInputDialog(errorMessage));
    return (result >= MIN && result <= MAX) ? result : companySize(ERROR_MESSAGE);
}

and

public static void main(String[] args) {
    UserInput getResult = new UserInput();
    int company_size =  getResult.companySize();
}
LowLevel
  • 1,085
  • 1
  • 13
  • 34