Sam-I-Am on Web Development

Sam Foster on the web and web-ish software developmen

Friday, February 27, 2009

A rhino prompt

I'm tinkering with another ill-conceived friday night project that may or may not see the light of day. But in the meantime, I just put together this little snippet that illustrates a lot of what's to like about rhino:
var getInput = function() {
 var br = new java.io.BufferedReader(
  new java.io.InputStreamReader(java.lang.System["in"])
 );
    return br.readLine();
};

var greetUser = function() {
 // use out.print for the prompt
 // instead of rhino's print - which is really println
 java.lang.System.out.print("Your name? ");
 var name = getInput();
 print("Hello " + name);
};

greetUser();
quit();
Arguably (and laughably if that's your attitude) there's exactly nothing special here - a whole lot of lines of code to do what in a browser (not to mention any other self-respecting scripting language) is done with one:
alert("Hello: " + prompt("Your name? ", ""))
But the point is that Rhino doesn't provide a prompt function, and it doesnt matter because you can easily make one. I'll take flexibility and potential like this over cute predefined functions every time.

Labels: