4

Hi I'm a beginner of Java lauguage.

It seems like my computer does not recognize FileReader at all.(Random class does not work either.) I typed the exact same code in a different computer and it worked. I uninstalled JDK and reinstalled it, but still doesn't work. I don't know what to do.

My environment

Samsung Netbook N150 plus. /// windows 7 starter/// java(1.6_21 standard edition) /// jGrasp(1.8).

Here is my code.

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

public class FileReaderGG
{
    public static void main(String[] args)throws Exception
    {
        FileReader infile = new FileReader("todolist.txt");

        Scanner indata = new Scanner(infile);

        while (indata.hasNextLine())
        {
            System.out.println(indata.nextLine());
        }
        infile.close();
    }
}

It gives me errors saying "cannot find symbol"

Looks like this FileReaderGG.java:11: cannot find symbol symbol : constructor FileReader(java.lang.String) location: class FileReader FileReader infile = new FileReader("todolist.txt");

5 more errors are there. I spent a whole day trying to figure out what the problem is. Please help me out.

3
  • The code is correct, compiles and runs correctly. Could you check java -version ? There may be some other java in the path? Commented Sep 22, 2010 at 6:52
  • My version is "1.6.0_21" checking from command prompt. Commented Sep 22, 2010 at 12:43
  • I downloaded JDK from here. oracle.com/technetwork/java/javase/downloads/index.html Commented Sep 22, 2010 at 12:44

3 Answers 3

2

It means that you are trying to use a constructor that isn't there. Apparently you are trying to input a String into the constructor, but there is no constructor that accepts just a String value, but that is not true for java.io.FileReader. Is there another class in the same package (folder) called "FileReader"? If so, line 8 should be

java.io.FileReader infile = new java.io.FileReader("todolist.txt");

instead. Other solutions include

public class FileReaderGG
{
  public static void main(String[] args) throws Exception
  {
    String pathName = System.getProperty("user.dir") + (FileReaderGG.class.getPackage() == null ? "" : "\\" + FileReaderGG.class.getPackage().getName().replace('.', '\\'));

    java.io.FileReader infile = new java.io.FileReader(pathName + "\\todolist.txt");

    java.util.Scanner indata = new java.util.Scanner(infile);

    while (indata.hasNextLine())
    {
      System.out.println(indata.nextLine());
    }
    infile.close();
  }
}

Note how no imports are made and all packages are explicitly declared. This should work no matter what. Just so you know, line 5 gets (A) the path from which the program is being run (hopefully the same as the resource file) and (B) checks if it is in a package and adds the needed sub-folders (though, it seems you aren't in any so it probably isn't needed)

Sign up to request clarification or add additional context in comments.

3 Comments

instead of pathName +"\\todolist.txt", use new File(folder, name) - this ensure the correct path seperator, and is also accepted by FileReader. You can also use File.pathSeperator, as that is "/" on Unix.
nod Good point. Thanks for the advice! I'll be sure to remember it :3
oh my god... I had a file named FileReader.java. As soon as I delete it, it works. Thank you!!
0

I think your code is 100% right. Its working on my end at least. Are u compiling this program from IDE or from command line?

1 Comment

It's from IDE (jGrasp). I had a file name that is exactly same as the FileReader class(FileReader.java). As soon as I delete that file, it worked. Thank you! :)
0

I think you have to import more, here's what I mean:

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

You know that when you

import java.util.Scanner;

It only imports the "Scanner" package, but not other packages in the Scanner package.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.