java - How do I take only integer inputs (with only scanner class and if and else statement or while loops if possible - no booleans)? -


here code have far:

import java.util.scanner; public class whatever{     public static void main(string[] args) {     scanner keyboard = new scanner (system.in);     system.out.println("how many pigs there?");     int number = integer.parseint( keyboard.nextline() );     int continueprogram = 0     while(continueprogram == 0)         {             if (number>= 0 && number <= 32767)             { this;                       continueprogram++;             }else{                      this;             } 

i have use integer.parseint rest of code work can't change that. ways take integers rather letters? code produces errors because if input letter, parseint produce red errors rather output string "try again. input numbers please" or that.

you need surround parse.int try catch this

int number = 0; // need initialize variable first while (true) {     try {     number = integer.parseint(keyboard.nextline());     break; // escape while loop     } catch (exception e) {     system.out.println("that not number. try again.");     } } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -