-2

I don't understand this: I have "Text.txt" in the same directory as my java files but my code keep throwing a java.io.FileNotFoundException: Text.txt (No such file or directory). Could somebody please tell me if I did anything wrong? I'm only concerned why my code is throwing an exception. Thanks in advance!

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class PrintLongestLines {

    public static void main(String[] args) throws FileNotFoundException{
        Scanner file = new Scanner(new File("Text.txt"));
        int numberOfLines = file.nextInt();
        String biggestLine = "";
        String[] myArray;
        int count = 0;
        int index = 0;

        while(file.hasNextLine()){
            if(!file.hasNextInt()){
                count++;
            }   
        }

        myArray = new String[count];
        while(file.hasNextLine()){
            if(!file.hasNextInt()){
                String line = file.nextLine();
                myArray[index++] = line;
            }   
        }
        String[] resultArray = new String[numberOfLines];
        for(int i = 1; i < myArray.length; i++){
            if(myArray[i].length() > myArray[i-1].length()){
                biggestLine = myArray[i];
            }
            else{
                biggestLine = myArray[i-1];
            }
        }
        resultArray[0] = biggestLine;
        for(int i = 0; i < myArray.length; i++){
            if(myArray[i].length() > myArray[i-1].length() && !myArray[i].equals(resultArray[0])
                                                            && !myArray[i-1].equals(resultArray[0])){
                biggestLine = myArray[i];
            }
            else if(myArray[i].length() < myArray[i-1].length() && !myArray[i].equals(resultArray[0])
                    && !myArray[i-1].equals(resultArray[0])){
                biggestLine = myArray[i-1];
            }
        }
        file.close();
    }
}
Maximillan
  • 59
  • 3

2 Answers2

2

The path (at run-time) isn't the source folder. You can check the runtime path with something like,

try {
    System.out.println(new File("Text.txt").getCanonicalPath());
} catch (IOException e) {
    e.printStackTrace();
}

And you might instead read relative to a user's home folder. Something like,

String home = System.getProperty("user.home");  
System.out.println(new File(home, "Text.txt").getCanonicalPath());
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

According to your comment.

Your text file is inside /home/username/workspace/Practice/src which is source folder of your project and new File("Text.txt") will look into root folder of your project which is /home/username/workspace/Practice. You can move your file to Practice.

Or you can find file in following way inside the source folder, with the help of class loader.

Scanner sc = new Scanner(PrintLongestLines.class.getClassLoader()
                .getResourceAsStream("Text.txt"));
Community
  • 1
  • 1
akash
  • 22,664
  • 11
  • 59
  • 87