wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Problem on Counting Characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Problem on Counting Characters" Watch "Problem on Counting Characters" New topic
Forums: Beginning Java I/O and Streams

Problem on Counting Characters

Anand Damodaran

Joined: Oct 04, 2012
Posts: 21

Input: JavaRanch // 9 + 1(for '\n')
But Output: 11

1> What is the 11th character? or is there any '\0' character?
2> Whether the read() used here, read characters from the keyboard BUFFER until NewLine Character is given ?

With respect,
Anand Damodaran

Anand Damodaran

Joined: Oct 04, 2012
Posts: 21

I found that characters '\u000D' - int value 13 and '\U000A'- int value 10(for '\n') were appended to the array of characters.

i.e., JavaRanch\u000D\U000A ,

so that i get 11.

What is the character value for \u000D? i get NewLine. is that True?

With respect,
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 7501

Anand Damodaran wrote:What is the character value for \u000D? i get NewLine. is that True?

I think so, but the reason you're getting two characters (almost certainly) is that you're running the program on Windows, which uses the CRLF form for an end-of-line ('LF' stands for 'line feed' which, as I recall, is the same as a Unix newline).

Instead of using an InputStream, which is very low level, why not just use a Scanner (java.util.Scanner) and read in a line at a time? Then you won't have any of those annoying control characters to worry about.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37926
Agree: the read() methods are low level and are not at all nice to work with.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41062
And if that doesn't confuse you, consider this: http://weblogs.java.net/blog/tomwhite/archive/2005/03/counting_charac.html. Counting characters is actually more complicated if you can't rule out arbitrary characters.

Ping & DNS - my free Android networking tools app
jQuery in Action, 2nd edition
subject: Problem on Counting Characters
Similar Threads
Byte vs Character streams
reading byte array
read chunks instead of lines with bufferReader
File IO problems
File Reader