Reputation: 49
So I just began learning Java FileI/O and was playing around with Input Stream Reader. However, the output for an exercise that I did was very strange and also did not match the guide I was following.
public static void main(String[] args)
throws InterruptedException
{
InputStreamReader cin = null;
try {
cin = new InputStreamReader(System.in);
char s = 0;
while (s != 133) {
s = (char) cin.read();
System.out.println(s);
}
} catch (IOException e) {
System.out.println("File IO Error");
} finally {
try {
cin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The code is supposed to just print the character, but it also prints a bunch of line breaks.
a
a
<linebreak>
<linebreak>
<linebreak>
When I cast the char to an int, it outputs the character id, and then 13 and 10.
a
97
13
10
Does anyone know what the problem is + how to fix this?
Upvotes: 0
Views: 123
Reputation: 14062
One way to fix this is adding:
if(System.getProperty("os.name").startsWith("Windows")){cin.skip(2);} // for Windows
else{cin.skip(1);} // for unix-like OS
after the System.out.println(s);
line, in this case your program skips the two chars Line Feed
and the Carriage Return
coming from Enter
.
Upvotes: 0
Reputation: 5232
read()
will read single character, when you press enter it reads carriage return (new line feed) as well and outputs it's representation as well.
Replace
System.out.println(s);
with
System.out.print(s);
In general InputStreamReader
is low level, it is recommended to use wrappers such as BufferedReader
as follows (it will solve your question as well)
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String tLine = null;
// now reading line of characters (delimited by carriage return)
while ((tLine = br.readLine()) != null) {
System.out.println(tLine);
}
Another suggestion is to use try-with-resources
, instead of traditional try-catch block.
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
// your code
}
// Resource will be closed automatically at this line
Upvotes: 2
Reputation: 21
System.out.println() not only prints out your string but it also advances to a new line. In Java, this is done by using both a carriage return and a line feed. A carriage return has an ASCII value of 13 and a line feed has an ASCII value of 10, which is why you see a 13 and a 10 returning too.
I'm not an expert at Java but I think the reason you are getting three linebreaks is because when you enter a character into the input stream reader, you not only enter the character but you also press the Enter key to input the character. So if you enter the character 'a' and press Enter, then your program will output the character a and then print a linebreak since it's a System.out.println(). It will then output the Enter character, causing another linebreak, and then print a third linebreak (since it's a System.out.println).
Upvotes: 0
Reputation: 159086
Your program is asking for input. To provide that input, your press a
then hit <enter>
.
That <enter>
key becomes the character CR
(decimal 13) and LF
(decimal 10) in the input stream.
The result is that your program reads 3 characters.
Upvotes: 0