Eclipse question from newbie - reading keyboard
I just installed Eclipse and am wondering if the following is normal. When I run a java program in Eclipse it cannot "read the keyboard" but it can print variable values okay. My program works fine if instead of using the Eclipse IDE I instead run in a cmd window in XP.
So it is the while loop at the bottom of my code that is ignored by Eclipse. I'm a newbie and am wondering if this is normal? Thanks. Maybe I must run it a special way?
Code :
import java.text.DateFormat;
import java.util.*;
public class eliz9
{
public static void main(String[] args)
{
int purp =2222;
System.out.println(purp);
Scanner keyboard = new Scanner(System.in);
String[] four = {"Tell me more about"};
System.out.println( System.currentTimeMillis() );
double a = System.currentTimeMillis();
int z=0;
while ( z < 999922227)
{
z++;
}
System.out.println( System.currentTimeMillis() );
double b = System.currentTimeMillis();
double c= b-a;
System.out.println( c);
z = 5;
while ( z < 7)
{
Scanner myScanner = new Scanner(System.in);
int xinputNumber = myScanner.nextInt();
z= xinputNumber;
}
// TODO Auto-generated method stub
}
Re: Eclipse question from newbie - reading keyboard
What inputs are you trying and what outputs are you expecting vs. what you are getting?
Re: Eclipse question from newbie - reading keyboard
Hello - in the cmd window the "while" loop will accept a key such as "1" or "2" and break out with "7" or greater and this works. But in Eclipse my keystroke inputs are not read, it seems.
Re: Eclipse question from newbie - reading keyboard
Did you press the return key? The command line will not send data to your program until it's been flushed (i.e. the return key has been pressed).
A suggestion: Don't create a new scanner every time through the loop. Create one scanner object before entering the while-loop, then continually read from that one scanner object. It shouldn't affect the final output of the code this time, but it is something you should get in the habit of doing as it could affect both performance and functionality at other times.
Re: Eclipse question from newbie - reading keyboard
I'm thinking you just need to click in the console window at the bottom of eclipse to give it focus. If it does not have focus then it won't take input.
Re: Eclipse question from newbie - reading keyboard
Thanks to all. I fixed the Scanner and used the focus and it now works as expected. Cheers.