Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Eclipse question from newbie - reading keyboard

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?



    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
     
    	}


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Eclipse question from newbie - reading keyboard

    What inputs are you trying and what outputs are you expecting vs. what you are getting?

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  5. #5
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default 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.

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Newbie Java Question
    By tinkersp in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 23rd, 2011, 02:41 PM
  2. Addition of doubles (newbie question)
    By archyb in forum Java Theory & Questions
    Replies: 2
    Last Post: April 21st, 2011, 10:52 AM
  3. Newbie Question on a Code - I don't know if this is Java
    By fresca in forum Java Theory & Questions
    Replies: 4
    Last Post: April 7th, 2011, 09:39 PM
  4. newbie question about Abstract methods
    By FailMouse in forum Java Theory & Questions
    Replies: 3
    Last Post: August 11th, 2010, 12:51 AM
  5. Looping Question (newbie)
    By xdrechsler in forum Loops & Control Statements
    Replies: 13
    Last Post: July 19th, 2010, 10:12 PM