2

I'm pretty sure there is a simple reason for this, but after combing through google hits I can't figure it out.

Problem: I am trying to read from a .dat file I created and placed in the src folder of the java project, but eclipse doesn't recognize it.

Things I have tried, 1. refreshing project. 2. placing file manually in many places. 3.saving and restarting.

Data File

2
12087 400
7418 978

Code

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

public class Distance {

public static void main(String[] args) throws IOException {

Scanner q = new Scanner (new File("distance.dat"));

int count = Integer.parseInt(q.nextLine().trim());

System.out.println(count);

   }

}

Package Explorer Package Explorer

Debug Error Debug Error

f6e9a
  • 61
  • 1
  • 8

1 Answers1

1

To me, it looks like distance.dat is in the src folder, which means you would need to do

public static void main(String[] args) throws IOException {
    Scanner q = new Scanner (new File("src/distance.dat"));
    int count = Integer.parseInt(q.nextLine().trim());
    System.out.println(count);
}

This is because Eclipse starts in the project folder, not the src folder.

My favorite way to debug this is to do:

public static void main(String[] args) throws IOException {
    File f = new File("src/distance.dat");
    System.out.println(f.getAbsolutePath());  //debug here that it's point to the right file
    Scanner q = new Scanner (f);
    int count = Integer.parseInt(q.nextLine().trim());
    System.out.println(count);
}
KevinL
  • 1,238
  • 11
  • 28