0

I'm now writing a java class and want to read a txt file in, like this:

public class Myclass {
...
public static void main(String[] args) {
    try{
        File file = new File(args[0]);
        Scanner scanner = new Scanner(file);
        int [] array = new int [1000];
        int i = 0;
        while(scanner.hasNextInt())array[i++] = scanner.nextInt();}
    catch (FileNotFoundException exp) {
        exp.printStackTrace();}
}
...
}

And for example, use it like java Myclass input.txt. However when I use javac to compile it in Linux, erros throws:

error: cannot find symbol
        catch (FileNotFoundException exp) {
               ^
symbol:   class FileNotFoundException
location: class Myclass

That's weird since name of input file hasn't even been passed in. I've tried File file = new File('input.txt'); and it also throws this error, so I don't know what's wrong (System.out.println(new File('input.txt').getAbsolutePath());will print out the correct and existed path).

Dana Mark
  • 45
  • 7

6 Answers6

1

You need to add correct imports at the begining of a class:

import java.io.FileNotFoundException;

public class Myclass {...
1

i think you have to compile your class using the following command

javac com/Trail.java
javac <package-name1>/<package-name2>/<classname.java>

then run the following command

java com.Trail test.txt

the you have to ensure test.txt place then it will works for you, let me recommand you the following answer of the question it helps me a lot to run your code here and here for where you should put your file

note:

  • try to declare public static void main(String args[]) throws FileNotFoundException

  • please you have to be inside folder of file which you want to compile

fathy elshemy
  • 481
  • 5
  • 18
  • Thanks for providing a new direction :D. Could you please tell me what is `com/Trail.java`, and `javac //` stands for which format (say `javac java.util.Arrays java.util.Random classname.java`is wrong). I've tried to add `-cp`(classpath) flag as mentioned in the first href, but still failed in compilation. And the second href seems to be lost. – Dana Mark Oct 31 '19 at 14:02
  • ``` com/Trail.java ``` is the package name of your file there is 2 correct syntax to write it and please you can read the following [link](https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean?page=1&tab=votes#tab-top) and will explain you every thing – fathy elshemy Oct 31 '19 at 14:07
  • please you have to be inside folder of file which you want to compile – fathy elshemy Oct 31 '19 at 14:09
  • Thx. I declare `public static void main(String args[]) throws FileNotFoundException`, and now it works, finally XD – Dana Mark Oct 31 '19 at 14:20
0

It looks like you did not import the class FileNotFoundException.

Adding a import java.io.FileNotFoundException at the top of your file should resolve the issue.

oktapodia
  • 1,695
  • 1
  • 15
  • 31
  • I tried to add `import java.io.FileNotFoundException`, and now ```Scanner scanner = new Scanner("array.txt"); int [] array = new int [1000]; int i = 0; while(scanner.hasNextInt())array[i++] = scanner.nextInt();```can pass the compilation but still cannot read the file (array all 0). Whilst for ```File file = new File(args[0]); Scanner scanner = new Scanner(file);```, it failed in javac compilation, with `unreported exception FileNotFoundException;`. – Dana Mark Oct 31 '19 at 12:45
0

Assuming all the imports are alright, the next most likely cause is the txt file is not there. You have to put your txt file at the same folder that there are folders like "src", "dist" and "build".

Ssr1369
  • 348
  • 3
  • 16
  • Or try an absolute path to see if this is the issue – Gert Kommer Oct 31 '19 at 12:45
  • I've tried absolute path but still caught. As for the relative path, my .java file is in, say, folder A, then I run `cd A` `javac my.java`. And I think it's not path that matters, since I tried `File file = new File(args[0]); Scanner scanner = new Scanner(file);` and just failed in `javac` compilation. – Dana Mark Oct 31 '19 at 12:53
  • "I don't think path matters" - are you saying the error happens in compilation time, not in running time? If so, that would be you missing an import, or your linker not finding the libraries. – Ssr1369 Oct 31 '19 at 12:55
  • Exactly. This is my whole imported libraries: ```import java.util.Arrays; import java.util.Random; import java.util.concurrent.*; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException;``` – Dana Mark Oct 31 '19 at 12:57
0

I've figured it out!

declare main like this:

public static void main(String args[]) throws FileNotFoundException{
...
Dana Mark
  • 45
  • 7
0

Try to put the location of file that you want to read, something like this:

File file = new File("C:\\text.txt");

Full example:

public static void main(String[] args) 
  { 
    File file = new File("C:\\text.txt"); 
    Scanner sc = new Scanner(file); 

    while (sc.hasNextLine()) 
      System.out.println(sc.nextLine()); 
  } 
besartm
  • 558
  • 1
  • 7
  • 14