AirCode
AirCode

Reputation: 3

How do I get my Java program to read the text file I want?

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)

Upvotes: 0

Views: 147

Answers (5)

Nagesh Soni
Nagesh Soni

Reputation: 11

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"));

Upvotes: 0

Juan Camilo Mejia
Juan Camilo Mejia

Reputation: 1012

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

Upvotes: 0

pokeyOne
pokeyOne

Reputation: 322

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.

Upvotes: 1

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

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

Upvotes: 0

Kevin Workman
Kevin Workman

Reputation: 42176

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

Upvotes: 1

Related Questions