Results 1 to 4 of 4
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default A little confused by this tiny program...

    This is the program:

    Java Code:
    public class EchoText 
    { 
       public static void main(String[] args) throws java.io.IOException 
       { 
          System.out.println("Please enter some text and press Enter!"); 
          int ch; 
          while ((ch = System.in.read()) != 13) 
             System.out.print((char) ch); 
          System.out.println(); 
       } 
    }
    The part that confuses me is in the while loop...
    basically, i tye in "a"
    then "b" it waits till I hit enter... then prints out "ab"
    how come "ch" is not getting overwritten on every keypress?
    I mean how come it does not only print out the last character (in this case it woud be "b")

    I dont see any concatenation above...

  2. #2
    buildakicker is offline nOOb :)
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    Is this what you mean?
    Java Code:
     public static void main(String[] args) throws java.io.IOException 
    	   { 
    	     
    	      Scanner myScanner = new Scanner(System.in);
    	      int ch;
    	      System.out.println("Please enter some text and press Enter: ");
    	      
    	     if(myScanner.findInLine(".").charAt(0) != 13){ 	       
    	    	 ch = myScanner.nextInt();
    	    	 System.out.println(ch);
    	      }
    	   }

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    how come "ch" is not getting overwritten on every keypress?
    The OS doesn't send any input to the read routine until Enter is pressed. Then it sends the whole line.

    I dont see any concatenation above
    The print() method you are using does not go to the next line after printing. So all the chars are on the same line.
    Change it to println() and the characters will be on separate lines

  4. #4
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The OS doesn't send any input to the read routine until Enter is pressed. Then it sends the whole line.
    Ah! That makes sense now, thanks!

Similar Threads

  1. HELP!!! Completely confused on how to fix this program!!!
    By BeginnerJava in forum New To Java
    Replies: 17
    Last Post: 06-12-2011, 11:59 PM
  2. Confused on how to start this Math program
    By gnng in forum New To Java
    Replies: 3
    Last Post: 04-16-2011, 12:14 AM
  3. Client Server program, a tiny problem.
    By skarosg3 in forum Networking
    Replies: 12
    Last Post: 05-27-2010, 01:03 PM
  4. 'Tiny' 3D Problem
    By Supamagier in forum Advanced Java
    Replies: 2
    Last Post: 09-03-2009, 09:34 AM
  5. Tiny Eclipse 3.3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-09-2007, 04:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •