Console input function for rhino?

How to accept a variable from console using javascript in Rhino? nothing like cin or scanf?

+7
source share
5 answers

Here are two lines that will do what you want:

var stdin = new BufferedReader( new InputStreamReader(System['in']) ) var aLine = stdin.readLine(); 
+7
source

In Rhino, you need to remember importing Java packages before you can use them. In addition, Java String is different from the built-in JavaScript string, so you can use it.

Here is a quick and dirty readln() that works the same in both SpiderMonkey and Rhino :

  var readln = (typeof readline === 'function') ? (readline) : (function() { importPackage(java.io); importPackage(java.lang); var stdin = new BufferedReader(new InputStreamReader(System['in'])); return function() { return String(stdin.readLine()); // Read line, }; // force to JavaScript String }()); 
+1
source

Just use the Java class library. I think this will work:

 var stdin = java.lang.System.in; var line = stdin.readLine(); 

At this point, it's easy to convert a string to whatever type you like, or break it into pieces using RegExp.

This may distort Unicode input, but I'm not sure if this is good, cross-platform.

0
source
 var ins = java.lang.System.in; var newLine = java.lang.System.getProperty("line.separator"); var is = new java.io.InputStreamReader(ins); var sb=new java.lang.StringBuilder(); var br = new java.io.BufferedReader(is); var line = br.readLine(); while(line != null) { sb.append(line); sb.append(newLine); line = br.readLine(); } var stdin = ""+sb.toString();//java string != javascript string console.log("stdin:"+stdin); 
0
source

Hope this helps you:

A simple function that reads a line from the console

 function readline() { var ist = new java.io.InputStreamReader(java.lang.System.in); var bre = new java.io.BufferedReader(ist); var line = bre.readLine(); return line; } print("Name? "); var name=readline(); print("Your name is: "+name); 
0
source

All Articles