File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Line by line counting of characters

 
Aasha Karthik
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

This is my first post on joining the forum. I feel encouraged that there are people out there to discuss and help.
I have been trying to create a mini project that takes an input file text file and performs some string manipulations on it using java code and stores the updated contents in an output file.

String manipulations:
1.Count the no of characters in each line.
2.Count the number of 4's in each line.
3.Replace 'this' with 'that'
4.Reverse line by line

My code prints the total number of characters and 4's in the file and not line by line. Could anyone suggest me on this please.

My function for counting number of 4's:


My function for counting total number of characters:



My main function is:


Also I would like the result from file1.replaceString(file1.copy()); in my main function to be passed as an argument to the following reverse function in main like file1.reverse(file1.replaceString(file1.copy())); But it wouldn't work. T

Thank you.
 
Winston Gutkowski
Bartender
Pie
Posts: 9464
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aasha Karthik wrote:This is my first post on joining the forum...

Welcome to JavaRanch Aasha.

First: When you're posting code, please UseCodeTags (←click). I've added them for you this time; see how much better it looks?

Also: avoid very long lines, and make sure your code is indented properly after you've pasted it. Yours is a bit off, but you can use the 'Edit' button to clean it up if you like, and the 'Preview' button to see what it will look like before you post. I'd also suggest NOT using tabs for indentation.

Thanks

Winston
 
Winston Gutkowski
Bartender
Pie
Posts: 9464
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aasha Karthik wrote:My function for counting number of 4's:...

It seems a bit specific to me. What if you wanted to change your program to count '5's instead? Or 4's AND 5's? Can you think how you might make that method a bit more generic?

It also contains almost identical code to your countChar() method
Can you think how you might join the two together?

Also: What is FileRead? I don't see any sign of such a class.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic