views:

83

answers:

2

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.

A: 

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?

EarnWhileLearn
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! :)
fiveroof
+1  A: 

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)

Supuhstar
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.
Staale
*nod* Good point. Thanks for the advice! I'll be sure to remember it :3
Supuhstar
oh my god... I had a file named FileReader.java. As soon as I delete it, it works. Thank you!!
fiveroof
I'm glad I could help!
Supuhstar