This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do i go about this...

 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a method called sizeFile() declared as follows:

public int sizeFile (String label) {
...
}

which counts the number of characters in the file whose name is supplied as the "label" parameter. This method should return the number of characters in the file, or -1 if an IOException occurs.


I'm not looking for someone to answer this for me, i'm just looking for hints, tips or examples of how i can go about this.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by study of the java.io.File class, learn how to open a file by name.
Next look at the various classes in java.io for reading files.

Bill
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill, it's not clear to me why you say the File class. Wouldn't the FileReader class be more appropriate? regards.

http://java.sun.com/docs/books/tutorial/essential/io/streams.html

If you are going to use files, you'll need to know a little about handling exceptions. the link discusses this.

Also, if I was keen to get started on the algorithm, I'd probably set up a string to mirror the file, and read characters from the string one at a time. You could switch to files later.

I can see you making use of the contains() method of the String class.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the Scanner class, but you will have difficulty getting your IOException.

I think people have told you enough for you to get it working.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please read this FAQ about thread titles.
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys, all your post have been noted.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Written up a code, just need to test it.
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is the code, but is says "cannot find symbol - method length()".

Anyone know what i'm doing wrong?
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karl Barnard wrote:

This is the code, but is says "cannot find symbol - method length()".

Anyone know what i'm doing wrong?


file is defined as an object in the FileReader class. In order for this to work, the FileReader class would have a length() method in it, or in a parent class. It does not. It does however have a read() method. If you follow the link I put up in an earlier post, you will see an example of how to use it.

length() will tell you the length of a string, but it won't tell you the length of a file.
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay i got it.
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
delete
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't delete text like that. People coming to this thread later won't know whether you have deleted something important or not. I happen to remember you said something like

"Thank you, am working on it"

but if the message is deleted, then we don't know whether there was something important which has been removed.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:length() will tell you the length of a string, but it won't tell you the length of a file.

length() will tell you the length of a file - if you call it on an instance of java.io.File, not java.io.FileReader.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Fred Hamilton wrote:length() will tell you the length of a string, but it won't tell you the length of a file.

length() will tell you the length of a file - if you call it on an instance of java.io.File, not java.io.FileReader.


duly noted, thanks, my suggestion of FileReader may not have been optimal either, I think I was confused about the exact nature of the task. Will endeavor to be more careful in the future.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic