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 items ordered”) Parameter 2 is a type of your 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

/** * @fileName :ReusableInt.java * @author * @since 10-02-2017 **/ import javax.swing.*; public class ReusableInt { /** * * @param prompt * @param range * @param lower * @param upper * @return */ public static int getInt(String prompt, int range, int lower , int upper){ int number; while (true){ try { number= Integer.parseInt(JOptionPane.showInputDialog(prompt)); if(range==1){ return number; } else if(range==2){ if(number=upper){ prompt=\"number should be less than or equal to \"+upper; continue; } else { return number; } } else if(range==4){ if(number>=upper && number<= lower){ prompt=\"number should be less than or equal\"+upper+\" and greater than or equal to \"+lower; continue; } else { return number; } } } catch (NumberFormatException nfe){ prompt=\"please enter a number\"; } } } public static void main(String[] args) { ReusableInt.getInt(\"Enter the number of items ordered\",2,10,20); } }
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 (ex

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site