I’m getting errors with this code that I don’t understand. I have everything set up in accordance with the Java documentation, but clearly I’m not understanding something. First, here is the code:
import java.io.FileNotFoundException;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
public class CreateTextFile {
private Formatter output; //object used to output text to file
public void openFile() throws FileNotFoundException {
try {
output = new Formatter( "sets.txt" ) { //First Error
@Override
public String format(LogRecord record) {
throw new UnsupportedOperationException("Not supported yet.");
}
}; //open the file
} catch ( SecurityException securityException ) {
System.err.println("You do not have access to this file.");
System.exit(1); //terminate the program
} catch (FileNotFoundException fileNotFoundException) { //second error
}
}
}
First Error:
constructor Formatter in class Formatter cannot be applied to given types;
required: no arguments
found: String
reason: actual and formal argument lists differ in length
I understand the error message, but I don’t agree with it. There is definitely a constructor for Formatter that takes a single string (see below)
Second Error:
constructor Formatter in class Formatter cannot be applied to given types;
required: no arguments
found: String
reason: actual and formal argument lists differ in length
I’m throwing the FileNotFoundException in the method declaration just as described in the Java documentation below.
This is straight from [http://doc.java.sun.com/DocWeb/api/java.util.Formatter][1]
Formatter(String fileName) throws FileNotFoundException Constructs a
new formatter with the specified file name.The charset used is the default charset for this instance of the Java virtual machine.
The locale used is the default locale for this instance of the Java
virtual machine.fileName The name of the file to use as the
destination of this formatter. If the file exists then it will be
truncated to zero size; otherwise, a new file will be created. The
output will be written to the file and is buffered.Throws SecurityException: If a security manager is present and
checkWrite(fileName) denies write access to the fileThrows FileNotFoundException: If the given file name does not denote
an existing, writable regular file and a new regular file of that name
cannot be created, or if some other error occurs while opening or
creating the file
Not sure how to clear these errors. Can anyone help? Thanks!
In your code:
In the URL of the Javadoc you are quoting:
It’s not the same class.