Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Character count from a file.

 
W Mckelvey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. I'm very new to java and need help figuring out how to count the number of times a specific letter is found in a file. I'm trying to read through a file test.txt and count the number of times the letter X is found in the file. I've written the following code to do this. I able to get the count of all the characters in the file but not the count of the specific letter. Am I on the right track at all?

 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you're on the right track. Just not all the way along the track yet.

You have some code which reads each line from the file. So far so good. Next you need some code which examines each character in that line. That would be some kind of for-loop, most likely. You don't have that yet. (You only have the code which wants to be inside that for-loop.)
 
W Mckelvey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this but it gets stuck in an infinte loop.

 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yeah. You have code which says "Keep going while j is such and such" and then you never change the value of j. So it keeps going. Forever, because j is always such and such.

But I would have written an ordinary for-loop which iterates over the characters in the string. With ordinary for-loops you don't need to worry about infinite loops, unlike with while-loops where you have to be careful to make them terminate at the right time.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
As for what Paul said about for loops, if you are stuck, try the Java™ Tutorials.
You can get out of your infinite while loop by moving the bit about j++ one line earlier; it is actually not in the loop.
Ignore that last bit; I was mistaken.
 
W Mckelvey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks for your help. i will try it.
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can get out of your infinite while loop by moving the bit about j++ one line earlier; it is actually not in the loop.
Ignore that last bit; I was mistaken.


I might have been mistaken too, I didn't see that line. (That's another reason for writing a standard for-loop, other programmers will understand it immediately, whereas with a while-loop like that it's easy to misunderstand.) However the posted code was apparently retyped from somewhere and not cut-and-pasted (I can tell because it has errors which won't compile) so it might not be the actual code being run.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:. . . I didn't see that line. . . .
That is actually because the quoted code was incorrectly indented.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic