the background: final loop simple program in user first inputs number of integer (double) command-line arguments, put them in array of length [args.length], , allow user enter integer , check whether in array. want loop end , program terminate when user enters string "exit".
but don't know how this. surely, can't hard solve this. (p.s. made static scanner method @ beginning it's not problem computer's 'you can't call on same mr. stdin twice, idiot!').
while (true) { double userstdin = stdin.nextdouble(); string exit = stdin.nextline(); if (contains(arguments, userstdin) == true) { system.out.println("this number in array."); } else if (contains(arguments, userstdin) == false) { system.out.println("this number not in array."); } if (exit.equals("exit")) { system.out.println("terminating."); return; } }
i need user able enter either number, or word "exit". how can in loop?
if need user able enter multiple types of data in single input, grab string , parse determine datatypes got.
let's assume scenario user can enter number or word "exit". other input invalid.
first, capture whatever user gives -- string, number, whatever:
string input = stdin.nextline();
then try parse use cases:
if("exit".equalsignorecase(input)) { // user entered exit system.exit(0); } // check see if it's number. there's number of ways this, simplicity's // sake, we'll try parse it. try { double number = double.parsedouble(input); // here number; } catch (numberformatexception e) { // not double, whatever was. // put error handling here, maybe message bad/unrecognized input }
note checking data types considered bad practice (eg. trying parse catching exception if fail.) i'm doing here illustration of technique.
Comments
Post a Comment