validation - Java validating a range of numbers from user input -


i have been having problem validation, , stressing me out, have done in various ways different loops, , 1 have of one, if take out if statement if function numbers, since program can accept numbers 1 5 want take step validating number , returning it. try declaring int option inside loop not carry out return statement.

    public int makeoption(){     int option;     system.out.println("choose following options: ");     system.out.println("(1)create entry || (2)remove entry || (3)list entry || (4)list || (5)exit ");      while(!reader.hasnextint()){             system.out.println("choose valid option.");             reader.next();     }     if(reader.nextint()<1 || reader.nextint()>5){         system.out.println("choose valid number options.");         reader.next();     }     option = reader.nextint();     system.out.println("you choosed: " + option);     return option; } 

swap out if statement switch statement:

int input = reader.nextint();  switch(input) {     case 1: //            break;     // blah blah blah more cases.      default: system.out.println("please enter correct value."); } 

note on code

when call reader.nextint(), you're telling stream expect int. you're not viewing same value. should store in variable, before using it.


Comments

Popular posts from this blog

How to set a border for android Imageview in Longpress? -

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -