• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem on Counting Characters

 
Anand Damodaran
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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,
Anand
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 49364
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree: the read() methods are low level and are not at all nice to work with.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic