Using the countChars() method from the previous answer, write a method called fileSize() declared as follows:
public int fileSize (String name) {
...
}
which counts the number of characters in the file whose name is supplied as the "name" parameter. This method should return the number of characters in the file, or -1 if an IOException occurs.
This is what i have done :
public int fileSize(String name) throws IOException
{
try{
int count = 0;
File file = new File("name");
Scanner scan = new Scanner(file);
while( file.exists())
{
count++;
}
return count;
}
catch (IOException e) {
return -1;
}
}
error: cannot find the class scanner.
Any suggestions would be much appreciated?
public int fileSize (String name) {
...
}
which counts the number of characters in the file whose name is supplied as the "name" parameter. This method should return the number of characters in the file, or -1 if an IOException occurs.
This is what i have done :
public int fileSize(String name) throws IOException
{
try{
int count = 0;
File file = new File("name");
Scanner scan = new Scanner(file);
while( file.exists())
{
count++;
}
return count;
}
catch (IOException e) {
return -1;
}
}
error: cannot find the class scanner.
Any suggestions would be much appreciated?
Comment