Java. System.in.read() and "\n" in console -


this question has answer here:

im creating java program should read console , print out, once again. code looks this:

import java.io.ioexception;  public class printer { public static void main(string[] args){     int i;     try {         while ((i = system.in.read()) != -1) {         char c = (char)i;          system.out.print(c);          }     }     catch (ioexception e) {         e.printstacktrace();     }  } } 

the problem is, if type text below in console first line printed, since "\n" after word "print" program doesn't print second line without me pressing enter manually

this text want print , pressed enter 

and when press enter, second line, result is:

this text want print  , pressed enter 

which not how looked like.

i prefer if first line didn't print automatically. want press enter , both lines @ same time. possible using while ((i = system.in.read()) != -1) do?

if c not equal new line character, print out?

if(c != '\n'){     system.out.println(c); } 

edit

in example below, terminate input full stop '.'. in loop, store input in string until terminates, print string contains both lines.

import java.io.ioexception;  public class main { public static void main(string[] args){     int i;     string line = "";     try {         while ((i = system.in.read()) != '.') {         char c = (char)i;          line = line + c;          }         system.out.println(line);     }     catch (ioexception e) {         e.printstacktrace();     }  } } 

my console looks this. first 2 lines input, last 2 output.

this text want print , pressed enter. text want print , pressed enter 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -