Phil
Phil

Reputation: 53

File Class error -

I'm following a tutorial and so far the example code was working. This is about I/O, and the provided code throws an error message:

EchoSquareDisk.java:8: error: constructor File in class File cannot be applied to given types;

File    file = new File("myData.txt");   // create a File object  
               ^

required: no arguments
found: String
reason: actual and formal argument lists differ in length
EchoSquareDisk.java:9: error: no suitable constructor found for Scanner(File)

Scanner scan = new Scanner( file );      // connect a Scanner to the file

Here is the code, copied from the tutorial:

import java.util.Scanner;
import java.io.*;

class EchoSquareDisk {
    public static void main (String[] args) throws IOException { 
        File    file = new File("myData.txt");   // create a File object
        Scanner scan = new Scanner( file );      // connect a Scanner to the file
        int num, square;

        num = scan.nextInt();
        square = num * num ;   

        System.out.println("The square of " + num + " is " + square);
    }
}

I have created a myData.txt file and put in it the character 9000, like I was asked.

Upvotes: 0

Views: 150

Answers (1)

Phil
Phil

Reputation: 53

Ok, being a beginner I have overlooked the already existing File.class file that was sitting in the same directory from another part of the tutorial.

I have renamed File.java to old_File.java as I suspected this be the cause for the error, but I didn't mind the File.class. Once I deleted File.class everything worked fine.

Upvotes: 2

Related Questions