我是新来的Java和编码一般,我只是无法得到我在这里搞砸了。我正在使用Eclipse,我试图从一个简单的文本文件中读取,其中包含两个数字“54 0.0”。我有一个文本文件“ClimateData”保存在src文件夹中这个java文件的旁边,它仍然不会运行程序。我不知道我做错了如何让我的Java程序读取我想要的文本文件?
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();
}
}
我得到这个错误:
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)
你得到什么错误 – brso05 2014-09-22 17:41:57
你的文件存储在哪里?它应该直接存储在您的项目下(根据您的代码)。它在吗? – TheLostMind 2014-09-22 17:41:58
你必须把你的文件放入classpath目录。尝试将其放置在工作区的顶部文件中。 – shinjw 2014-09-22 17:42:20