runtime error - java scanner does not accept two words with space in between -
i'm trying create simple project ask user true or false , if true scanner gonna keep storing category name in arraylist. , works fine until enter 2 words space in category name "first second" example reason jump condition true or false , check have entered word true or false , execute catch clause . i've tried using both scanner.next or nextline , nextline jump true or false without asking category name. please help. thank much.
public class mainclass { public static boolean check = false; public static void main(string[] args) { scanner in = new scanner(system.in); categories c = new categories(); system.out.println("please enter true if want add category \n or false if not"); try { check = in.nextboolean(); } catch (inputmismatchexception e) { system.out.println("sorry must put true or false!!!"); system.exit(0); } while (check) { system.out.println("please enter category name"); c.category.add(in.next().tostring()); system.out.println("do want add more true or false"); try { check = in.nextboolean(); } catch (inputmismatchexception e) { system.out.println("sorry must put true or false!!!"); system.exit(0); } } system.out.println("thank categories added"); system.out.println(c.category); }
}
you can use inputstreamreader wrapped around bufferedreader:
bufferedreader reader = new bufferedreader(new inputstreamreader(system.in)); reader.readline(); // read 2 words
Comments
Post a Comment