Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Input {
- public static void main(String[] args) {
- String prompt="Enter any integer in range";
- String error="Invalid Value! ";
- int min=1,max=100;
- Scanner in=new Scanner(System.in);
- int value=readInt(in,prompt,error,min,max);
- System.out.println("The Value is "+value);
- }
- public static int readInt(Scanner in,String prompt,String error,int min,int max) {
- in=new Scanner(System.in);
- int userInput;
- while(true) {
- System.out.print(prompt+" "+min+" and "+max+" :");
- userInput=in.nextInt();
- if(userInput>=min && userInput<=max) {
- return userInput;
- }else {
- System.out.println(error);
- }
- }
- }
- }
- OUTPUT:
- Enter any integer in range 1 and 100 :200
- Invalid Value!
- Enter any integer in range 1 and 100 :-90
- Invalid Value!
- Enter any integer in range 1 and 100 :70
- The Value is 70
Add Comment
Please, Sign In to add comment