-1

Im currently creating a java program which should read the console and print it out, once again. The code looks like 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 you type this text below in the console you will get the first line printed, but since its a "\n" after the word "print" the program doesn't print the second line without me pressing Enter manually

This is the text I want to print
And now I pressed Enter

And when i press enter, to get the second line, the result is:

This is the text I want to print

And now I pressed Enter

Which is not how it normally looked like.

I would prefer if the first line didn't print automatically. I want to press Enter and get both lines at the same time. Is that possible using while ((i = System.in.read()) != -1) like I do?

user207421
  • 289,834
  • 37
  • 266
  • 440
user3712130
  • 77
  • 1
  • 3
  • 7

1 Answers1

0

If c is not equal to a new line character, print it out?

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

EDIT

In the example below, I terminate the input with a full stop '.'. In the loop, store all the input in a String until it terminates, then print the String which now 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 like this. The first two lines are input, the last two are the output.

This is the text I want to print
And now I pressed Enter.
This is the text I want to print
And now I pressed Enter
ConMan
  • 1,582
  • 1
  • 12
  • 22
  • 1
    You should use `'\n'` here instead of `String` literal as here to be compared with `String` `c` would be autoboxed to `Character` and comparison would fail in any and every case. – Dmitry Ginzburg Jun 09 '14 at 07:18
  • apologies, of course you should compare char to char, edited. Good spot ;) – ConMan Jun 09 '14 at 07:27
  • the problem is not that it prints a new line. The problem is that it automatically prints the first part (the first line) and then I have to press enter to get the second line. I wish to press Enter once and get all the content. – user3712130 Jun 09 '14 at 09:07
  • edited, hope that answers your question! – ConMan Jun 09 '14 at 09:43
  • Is there any way to do this without exiting the loop at ".". I would like to end the loop when i press on Enter – user3712130 Jun 09 '14 at 10:03
  • The only way I know to terminate System.in is with ctrl + z, which probably isnt what youre looking for. Of course you cant use the enter key to exit the while loop, because then you wouldnt get the second line. – ConMan Jun 09 '14 at 10:25