Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Yet another "Cannot Find Symbol" Error  RSS feed

 
Andrew Farrell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently working on a little project.
I'm developing a virtual machine based on Von Neumanns Model.

I'm in the first stage of coding and working on a file reader and tokenizer to import the assembly code from a text file and then tokenize it.

One of the requirements for the pronject was to have all classes in seperate files. In the past I have always used one file with all of my classes contained within. This is my code for my File Reader:

//File Reader

import java.io.*;

class readFile
{

public String readFile(File f) throws IOException
{
FileReader in = new FileReader(f);
int size = (int) f.length();
char data[] = new char[size];
int chars_read = 0;
String s;

while(chars_read < size) {
chars_read += in.read(data, chars_read, size - chars_read);
}

in.close();
s = new String(data);
System.out.print(s);
return s;
}
}

This is the code for my tokenizer:

//Tokenizer Class

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

public class Tokenizer {
public static void main(String[] args) {
String s = new String("This is a sentence");
StringTokenizer st = new StringTokenizer(s);
int i = 1;
while(st.hasMoreTokens()) {
String token = st.nextToken();
System.out.println("Token " + i + ": " + token);
i++;
}
}
}

This is the code I was using to test the two classes:

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

class Test
{
public static void main(String[] args)
{
File f = new File(args[0]);
String s = readFile(f);
tokenize(s);
}
}


When I try to compile "Test.java" I get the following error:

Test.java:1: cannot find symbol
symbol: class readFile
location: package java.util
import java.util.readFile;

etc. etc.

Any idea what I'm doing wrong?
 
Jammy Wells
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all remove that import statement regarding readFile , you have assumed (or by mistake) your class readFile as part of java.util package which is incorrect....one more thing according to java naming conventions your class file name should start from capital letter ...your readFile should be ReadFile.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Your readFile class is not in the java.util package, so "import java.util.readFile" makes no sense. You should be able to remove this line.

Also, it looks like your readFile class is in a file named "Reader.java." It would help if the file name matched the name of the class it contains. (By convention, Java class names begin with an uppercase letter, so this should be "ReadFile.")
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh! I didn't see Jammy's response when I posted. So yeah, like Jammy said.
 
Andrew Farrell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the speedy reply.
I shall tinker with the code a bit and report back tomorrow afternoon.
thanks guys.

(PS: This assignment is going to be developed over the next two weeks, so I may have quite a few questions. Should I rename this thread and keep posting questions within it or should I clutter up the forums with lots of small questions?)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andrew Farrell:
...Should I rename this thread and keep posting questions within it or should I clutter up the forums with lots of small questions?)

If it's a follow-up to this "cannot find symbol" issue, then continue in this thread. Otherwise, it would be better to start a new topic for each issue so that each thread stays easy to follow.
 
Andrew Farrell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I correct to assume that as long as all of my class files are in the same directory I will be able to access the methods contained within the class files?

IE; I write "Filereader.java" and want to use it in a different file ie: "Test.java"

I should just be able to call on the methods of Filereader without adding any import statements to Test?
 
Jammy Wells
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew

Its better if you refer a basic java book while working on this assignment , it will really help you in finishing it....and yes we are always here for your doubts....
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andrew Farrell:
Am I correct to assume that as long as all of my class files are in the same directory I will be able to access the methods contained within the class files? ...

If you don't explicitly declare your source file to be part of a package, then the default is for the classes to be in an "unnamed package," which generally corresponds to the current directory (see JLS 7.4.2 Unnamed Packages). The current package -- which might be the unnamed package -- is automatically imported. So basically, if you don't explicitly name packages, then whatever is in the current directory should be visible.

(Of course, in order for methods to be accessible from outside the class, they cannot be marked private.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!