Code example for PrintStream

Methods: printf, println

0
 
public class ScriptFlipBindings { 
	public static void main(String args[]) throws IOException {
		ScriptEngineManager manager = new ScriptEngineManager();
 
		System.out.println("Please input a string:");
 
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(System.in));
		String readLine = bufferedReader.readLine();
 
		ScriptEngine engine = manager.getEngineByName("javascript");
		try { 
			engine.put("name", readLine);
			engine.eval("var output = '';"
					+ "for (i = 0; i <= name.length; i++) {" 
					+ " output = name.charAt(i) + output" + "}"); 
			String name = (String) engine.get("output");
			PrintStream console = System.out;
			console.printf("Reversed: %s%n", name);
		} catch (ScriptException e) {
			System.err.println(e);
		} 
	} 
}