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
Post a Comment