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