我是新來的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