-3

Hi I have created two Java file under a directory called Utility with as follow:

  1. FileName.java
public class FileName {
  private String name;
  public FileName (String name) {
      //some code
  }
  String getName() {
      return name;
  }
}
  1. FileNameReader.java
public class FileNameReader {

  public static void main(String[] args) throws IOException {
      FileName obj = new FileName("testfile.txt");
  }

}

Now, when i am comipling throw command prompt, compiler giving me error saying cannot file symbol at FileName obj = new FileName(); this line

Ashish Chauhan
  • 486
  • 2
  • 8
  • 22
  • 1
    Don't fix your code afterwards because it completely changes the context of the question. Right now, there shouldn't be any compiler error anymore, except for the missing import statement. – QBrute May 01 '17 at 09:47
  • @QBrute, I've deleted my answer because it does not make sense anymore – Manuel Schmidt May 01 '17 at 10:02

2 Answers2

0

Here:

public SearchFile(String name) {

That would be the constructor of a class called SearchFile.

But your class is named FileName; so you have have to rename everything (class and file name) to SearchFile; or you change the ctor to say public FileName(String name) instead.

Beyond that: that class only has a constructor taking a string argument. But your other class goes for new FileName() without providing an argument. That will not work either.

But the real answer here: such "subtle" details matter. You have to pay close attention to each and any line of code that you write down. And: you run the compiler immediately after you have "finished" something of which you think it should compile. You don't create 2, 3 files with 10, 20, 50 lines of code to then find out that you have a bunch of problems in each file!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Probably you have many different errors. Go file by file, start with those that don't depend on others. – GhostCat May 01 '17 at 09:02
0

You have to problems in your FileNameReader class:

  1. You need to import the IOException by adding

    import java.io.IOException;
    
  2. You have created an explicit constructor for FileName with a String argument, so the implicit default constructor is no longer visible. You need to provide that String when creating a new instance:

    FileName obj = new FileName("someString");
    

This FileNameReader.java compiles:

import java.io.IOException;

public class FileNameReader {

  public static void main(String[] args) throws IOException {
      FileName obj = new FileName("someString");
  }

}
Marvin
  • 13,325
  • 3
  • 51
  • 57