java.util.scanner - Terminate Java Program on Ctrl+Z input from user -


i have following code:

        string inputstring;         scanner in = new scanner(system.in);         while (true) {             system.out.print("jcp>");             inputstring = in.nextline();              string[] tokens = inputstring.split(" ");             switch (tokens[0]) {                 case "new":                     createnewinstance(tokens);                     break;                 case "call":                     callmethod(tokens);                     break;                 case "print":                     object obj = hashmap.get(tokens[1]);                     print(obj);                     break;                 default:                     system.out.println("illegal command!");                     break;             }         } 

i want program break out of while loop when user hits ctrl+z

ctrl+z exit program bad, ctrl+c|ctrl+d ends program execution on terminal, same way expect users type in commands, tell them type exit command exit program , shortcuts x or e can used instead of complete word.

how following code,

string inputstring; scanner in = new scanner(system.in); while (true) {     system.out.print("jcp>");     inputstring = in.nextline();     string[] tokens = inputstring.split(" ");     switch (tokens[0]) {         case "n":         case "new":             createnewinstance(tokens);             break;         case "c":         case "call":             callmethod(tokens);             break;         case "p":         case "print":             object obj = hashmap.get(tokens[1]);             print(obj);             break;         case "e":         case "x":         case "exit":             return;         default:             system.out.println("illegal command!");             break;     } } 

if return; won't work can replace system.exit(0);


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -