0

I'm new to Java and coding in general and I just can't get what I messed up here. I'm using Eclipse and I'm trying to get it to read from a simple text file that has two numbers in it "54 0.0" written just like that. I have the text file "ClimateData" saved right next to the this java file in the src folder and it still won't run the program. I don't know what I've done wrong

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

public class ClimateSummary
{
public static void main (String [] args) throws FileNotFoundException
{

    Scanner textFile = new Scanner (new File("ClimateData"));

    int inputTemperature = textFile.nextInt();
    double inputPercipitation = textFile.nextDouble();

    if (inputTemperature > 90)  {
        System.out.print("It's miserably hot out man, ");

    }   else if (inputTemperature < 90 && inputTemperature > 80)    {
        System.out.print("It's kind of hot, ");

    }   else if (inputTemperature < 80 && inputTemperature > 60)    {
        System.out.print("It's pretty nice out, ");

    }   else if (inputTemperature < 60 && inputTemperature > 32)    {
        System.out.print("It's a bit cold out, ");

    }   else if (inputTemperature < 32) {
        System.out.print("It's miserably c-c-co-cold bro, ");
    }

    if (inputPercipitation > .1)    {
        System.out.print("and it's a little rainy.");

    }   else if (inputPercipitation <= .1)  {
        System.out.print("it's not rainy.");
    }

    textFile.close();

 }
}

I get this error:

Exception in thread "main" java.io.FileNotFoundException: ClimateData (The system  cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at ClimateSummary.main(ClimateSummary.java:11)
AirCode
  • 3
  • 3

5 Answers5

1

Try this to see where Java is looking for your file:

System.out.println(new File("ClimateData").getAbsolutePath());

Then move the file to wherever Java is looking.

Alternatively, you can use the getResource() methods to look for the file relative to your class files. More info on that approach here: getResourceAsStream() vs FileInputStream

Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • 2
    @PM77-1 Sorry, but you are wrong. This will still print out the absolute path that it's looking at, even if the location isn't a valid file. This will show OP where Java is looking for the relative file, and he can move it there. Everybody else is telling him to just move it to the top level, which is probably correct, but this answer lets him figure that out for himself and is more robust, imho. – Kevin Workman Sep 22 '14 at 17:49
  • Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at ClimateSummary.main(ClimateSummary.java:13) – AirCode Sep 22 '14 at 17:55
  • 1
    @AirCode My line needs to go before any of the lines that throw an error. That error has nothing to do with the code I posted, and it means that wherever you moved the file to is now correct, but you're parsing it incorrectly. – Kevin Workman Sep 22 '14 at 17:57
  • @KevinWorkman Ok, thanks I'll try that out too. I was being dumb and didn't even notice it was print statement. – AirCode Sep 22 '14 at 18:02
  • I got it finally, thanks so much! It showed me the path and I just moved it there. I also changed the "ClimateData" to "ClimateData.txt" – AirCode Sep 22 '14 at 18:13
  • I was wrong indeed and that's I who should be sorry. I guess my brain wasn't fully engaged at the time. – PM 77-1 Sep 22 '14 at 20:52
1

you are getting the file not found error because the file is in the src folder you have to put "src/" in front of the path. Also you must put the file extension after it so it looks like this "src/ClimateData.txt". That should fix it.

pokeyOne
  • 312
  • 1
  • 2
  • 12
  • I ended up just moving the text file out of the src folder. But I saw that you had the .txt extension so I tried that and it finally worked! Thanks a lot yo! – AirCode Sep 22 '14 at 18:19
0

Move it out of your src folder and into the project root, as that is the default currend direcotry of any launch.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
0

Yoy have to check your path ,and remember in java if you are in windows, you write the path like this:

C:\\my folder\\mydocument.myextension
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Juan Camilo Mejia
  • 952
  • 3
  • 14
  • 28
  • Yeah, I had to move the text file and add the extension type to the code to get it to work. Thanks for the help! – AirCode Sep 22 '14 at 18:16
0

Scanner textFile = new Scanner (new File("ClimateData"));

Correction for your line is : Give file path specifying the source folder.

Scanner textFile = new Scanner (new File("src/ClimateData"));