Java Programming Reusable input method for an int Create a m
Java Programming
Reusable input method for an int
Create a method called getInt. It has four parameters:
Parameter 1 is a String, used for the input prompt (example: “Enter the number of objects ordered ”)
Paramete 2 is a type of our choice indicating 4 different range checking options:
1.No range checking
2.Only a lower bound range checking (inclusive)
3.Only an upper bound range check (inclusive)
4.Both lower and upper bounds checked (inclusive)
Parameters 3 and 4 are ints for the range lower and upper bounds, if used.
The method should use a dialog input box to get the int with the prompt provided. It should use a loop,
and a try - catch block, along with appropriate range checking, to ensure data validity. It then returns the
int. The prompt should be modified to include an error message if either the try - catch or the range checking flags an incorrect entry.
Solution
PFA the solution with the following doubt as the follwoing are not clear from the question:
1. Where to use the loop ? (Please find the comments added over the method)
2. Since dialog box is mentioned, so is GUI required?
package new_Cheg_2;
import java.util.Scanner;
public class RangeCheck {
   
   
 /*
 * CONFUSION::
 * Please be more clear on where the method needs the loop.
 * Is it that the method will be called in loop with different choices ?
 * OR
 * The loop will continue until the user says so? But with the same choice? When user eneters any
 * thing else other than Y or y then the int is returned and printed
 *
 * The constraint here is the method is supposed to return the entered int
 */
 public int getInt(String inputPrompt, int choice, int lowerBound, int upperBound) throws Exception{
int returnedValue = 0;
 boolean flag = true;
//loop for repeating the action till user says yes with same choice entered once
 while(flag){
   
 //Taking input from user
 Scanner scanner = new Scanner(System.in);
 System.out.println(inputPrompt);
 int enteredNumber = scanner.nextInt();
   
 //try-catch for validating the entry
 try{
 switch (choice) {
 case 1:
 returnedValue = enteredNumber;
 break;
 case 2:
 if(enteredNumber>=lowerBound){
 returnedValue = enteredNumber;
 break;
 }else{
 throw new Exception(\"Lower bound violated!!\");
 }
 case 3:
 if(enteredNumber<=upperBound){
 returnedValue = enteredNumber;
 break;
 }else{
 throw new Exception(\"Upper bound violated!!\");
 }
 case 4:
 if((enteredNumber >= lowerBound) && (enteredNumber <= upperBound)){
 returnedValue = enteredNumber;
 break;
 }else{
 throw new Exception(\"Upper and Lower bounds violated!!\");
 }
 default:
 break;
 }
 }catch(Exception e){
 throw new Exception(e.getMessage());
 }/*finally {
 if(scanner!=null){
 scanner.close();
 }
 } */
 System.out.println(\"Continue?? Y | N\");
 flag = scanner.nextLine().equals(\"Y\") || scanner.nextLine().equals(\"y\") ? true : false;
 }
   
 return returnedValue;
 }
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 RangeCheck ob = new RangeCheck();
 try{
 System.out.println(ob.getInt(\"Please eneter\", 2, 10, 50));
 }catch(Exception e){
 System.out.println(e.getMessage());
 }   
 }
 }


