Can anybody out there let me know whether, and if so how, Scanner can be used in the Greenfoot environment to receive keyboard input? I am seriously considering using Greenfoot to teach OOP and Java and would like to build a small bank of simple I/O examples first.
The following simple example does not work as intended. Sometimes it hangs (I'm thinking on the line that instantiates the class). Sometimes it prompts for a number and then hangs.
Any help would be appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; import java.lang.System; /** * Write a description of class Main here. * * @author (your name) * @version (a version number or a date) */ public class Main { /** * Constructor for objects of class Main */ public Main() { Scanner input = new Scanner( System.in ); System.out.println( "Enter a number" ); int n1 = input.nextInt(); System.out.printf( "Number is %d\n" , n1); } } |