java - How do I prompt a user until they enter a valid integer? -


valid integer being letter. don't know command make check strings, nor know find it. appreciated.

import java.util.scanner;  public class stringtest{  public static void main(string[] args) {     scanner input = new scanner(system.in);      int test = 10;      while (test>0){     system.out.println("input maximum temperature.");     string maxtemp = input.nextline();     system.out.println("input minimum temperature.");     string mintemp = input.nextline();     } } } 

use nextint() next integer value. should try/catch in case user types non-integer value.

here's example:

scanner input = new scanner(system.in); // infinite loop while (true){         system.out.println("input maximum temperature.");         try {             int maxtemp = input.nextint();             // todo whatever need max temp         }         catch (throwable t) {             // todo handle better             t.printstacktrace();             break;         }         system.out.println("input minimum temperature.");         try {             int mintemp = input.nextint();             // todo whatever need min temp         }         catch (throwable t) {             // todo handle better             t.printstacktrace();             break;         } } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -