-3

I need to write a program that asks for the file name of a text document of number and then calculates average, median, etc., from this data set. I have written the program so that runs correctly when I input the full path such as "C:\Users\COSC\Documents\inputValues2.txt", however it will not run when I simply input inputValues2.txt. I have been researching the different between the two but am not fully understanding how to fix this. Since it is running correctly, otherwise, I don't believe it is a problem with the code, but I am new to this so I may be wrong.

coscdummy
  • 153
  • 1
  • 9

2 Answers2

1

Your program needs to know the full path in order to find the file. It isn't just searching your computer for the file "inputValues2.txt". It needs to know exactly how to get there. If you wanted to, you could move the file into your project folder, and then you would just be able to write "inputValues2.txt" to access it. I normally create a folder called "res" in my project folder, and then let's say I am trying to create an image:

Image i = new Image("res/img.png");
John Farkerson
  • 2,543
  • 2
  • 23
  • 33
-1

Your file should be in the class-path. That's in the same directory that your main class is in.

The suggested practice is to place it in a Resources directory inside your class-path, then you can access it via, "Resources/inputValues2.txt".

Mordechai
  • 15,437
  • 2
  • 41
  • 82
  • Not the downvoter, but note the OP says "asks for the file name," which implies that the file is not known at build time. In addition, you may want your resources in packages just like your code, to avoid nameclash. – Andy Thomas Dec 04 '15 at 20:00