I'm was wondering if someone can check if this method produce the right result?

public int fileSize (String name) throws IOException
   {
       int charLength = 0;
       File file = new File("name");
       Scanner input = new Scanner(file);
       while (input.hasNext()) {
           charLength++;
        }
        return charLength; 
    }

What this is supposed to do is count the number of characters in the file whose name is supplied as the name parameter. The method return the number of characters in the file or -1 if an IOException occurs.

I'm not on my computer so i can't test it with the application.

Recommended Answers

All 16 Replies

No, it does not, because Scanner's hasNext() method does not get the next character, or get anything for that matter. It tells you if the Scanner has any more tokens in its input, and it does not advance past any input. So you'd be sitting in an infinite loop (if there were actually tokens). A token is just matched by the delimiter pattern, which by default, is whitespace. So if you had the following:

blah deee weeee

The first token is blah, the second is deee, and the third is weeee. Anyway, I don't see why you don't just construct the file Object then use the File's length method to see how many bytes are in the File, which tells you how many characters are in the file. Here is the documentation I am referring to when I'm talking about Scanner and File. Although it is a good idea to get familiar with both, so you don't need to look it up.
http://java.sun.com/javase/6/docs/api/java/io/File.html#length()
http://java.sun.com/javase/6/docs/api/java/util/Scanner.html#hasNext()

Alternatively, you can either look up FileReader here on Daniweb (this will bring you to other threads where people had similar questions to the one you are asking), or you can look at this documentation: http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileReader.html . In particular, consider using the read() method of the FileReader class after you construct your FileReader object. It reads a single character at a time.

The modification of your program, one way or another, to produce the correct result is only one or two lines. But I'm trying to help you understand how to use the documentation as well as learn about some additional ways to do this. The desired way to do this would probably be to just construct the File, then use the length method.

No, it does not, because Scanner's hasNext() method does not get the next character, or get anything for that matter. It tells you if the Scanner has any more tokens in its input, and it does not advance past any input. So you'd be sitting in an infinite loop (if there were actually tokens). A token is just matched by the delimiter pattern, which by default, is whitespace. So if you had the following:

blah deee weeee

The first token is blah, the second is deee, and the third is weeee. Anyway, I don't see why you don't just construct the file Object then use the File's length method to see how many bytes are in the File, which tells you how many characters are in the file. Here is the documentation I am referring to when I'm talking about Scanner and File. Although it is a good idea to get familiar with both, so you don't need to look it up.
http://java.sun.com/javase/6/docs/api/java/io/File.html#length()
http://java.sun.com/javase/6/docs/api/java/util/Scanner.html#hasNext()

Alternatively, you can either look up FileReader here on Daniweb (this will bring you to other threads where people had similar questions to the one you are asking), or you can look at this documentation: http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileReader.html . In particular, consider using the read() method of the FileReader class after you construct your FileReader object. It reads a single character at a time.

The modification of your program, one way or another, to produce the correct result is only one or two lines. But I'm trying to help you understand how to use the documentation as well as learn about some additional ways to do this.

Thanks for the advice BestJewSinceJC.

The part in bold, is that the best way to solve the problem to my method? Just curious.

All of the ways I mentioned are pretty easy, but the easiest way would be to make a File object (like you already did), and then use file.length(). file.length() returns the number of bytes in the file, which is the same as the number of chars in the file.

(Note: on some systems, chars may be two bytes, in which case, what I just said would be incorrect. Do a simple test with a small file and count it by hand to make sure)

Thanks again BestJewSinceJC.

I'm sorting the method out now.

Okay i edited the program...

{
       int charLength = 0;
       FileReader file = new FileReader("name");
       [b]while (file.length()) {[/b]
           charLength++;
        }
        return charLength; 
    }

I get an error that says "cannot find symbol - method length()".

Am i going the right way?

http://java.sun.com/javase/6/docs/api/java/io/File.html#length(

That is the documentation for the File class. You are trying to use the length() method with an Object of the class FileReader, when in fact, the length() method is a method of the class File. Go back and re-read my original post to you. You're obviously pretty confused, so I'll give you some code examples to help. Typically we aren't supposed to give away code on daniweb, but considering the simplicity of this, I'm going to assume you gave it effort and are just confused by my explanation. I can't really explain it any differently, so maybe seeing the examples along with the explanation will clarify things:


This is how you do it with a FileReader. Remember, with a FileReader, you are reading through everything in the file, character by character, and incrementing a counter. The final value of the counter is how many characters were in the file.

while((int i = fileReader.read()) != -1){
counter++;
}

Now, below is a different way of doing the same thing. Below is how you do it with a File Object. You already created a file Object, so I'll use the one you created, and call it's length() method. The length() method for the File class tells you how many bytes are in the file. Since a character is usually represented by one byte, this also tells you how many characters are in the file.

File file = new File("name");
System.out.println("Chars in the file = " + file.length());

Well it runs...

public int fileSize (String name) throws IOException
   {
       int charLength = 0;
       FileReader file = new FileReader("name");
       while (file.read() != -1) {
           charLength++;
        }
        return charLength; 
    }

But doesn't produce the result i want?

Know what's wrong with that?

You probably aren't creating the FileReader Object correctly, due to having an incorrect pathname for the file. For example, if your file's name was name.txt, but it was located on the Desktop, you would have to use "C:/Users/yourUsername/Desktop/name.txt" where you have "name".

Hmm. It's possible the code I gave you doesn't work. It's also possible that you aren't creating the FileReader Object correctly, due to having an incorrect pathname for the file. Hold on, let me do a quick test.

Hey BestJewSinceJC

I took out the "" in the FileReader("name") It works, but still not getting the exact result.

I look into some more.

I edited my post, re-read it. There is no way your file is just named "name". It would be named "name.txt" at the very least. And if it was not in the same directory as your .java file, you would need to specify the directory such as "C:/name.txt" or wherever it is.

I get an error that says...

An unhandled exception (class java.io.FileNotFoundException) occurred.
Try again!

I edited my post, re-read it. There is no way your file is just named "name". It would be named "name.txt" at the very least. And if it was not in the same directory as your .java file, you would need to specify the directory such as "C:/name.txt" or wherever it is.

Yeah, it works now.

Thanks for the help BestJewSinceJC. :)

Yeah, no problem. Mark the thread as solved

Sorry, but how do you do that?

by clicking the "mark as solved" blue link thats at the bottom of the last post

Alright, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.