Reputation: 41
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.
Upvotes: 4
Views: 10811
Reputation: 159
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.
Upvotes: 0
Reputation: 32073
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)
Upvotes: 2
Reputation: 1011
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?
Upvotes: 0