0

我有一个值列表(天气数据),编写列表的人在没有要报告的值时使用值“9999”。我导入了文本文件并使用以下代码获取数据并对其进行编辑:

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

public class weatherData {

public static void main(String[] args)
        throws FileNotFoundException{
    Scanner input = new Scanner(new File("PortlandWeather2011.txt"));
    processData(input);
}

public static void processData (Scanner stats){
    String head = stats.nextLine();
    String head2 = stats.nextLine();
    System.out.println(head);
    System.out.println(head2);
    while(stats.hasNextLine()){
        String dataLine = stats.nextLine();
        Scanner dataScan = new Scanner(dataLine);
        String station = null;
        String date = null;
        double prcp = 0;
        double snow = 0;
        double snwd = 0;
        double tmax = 0;
        double tmin = 0;
        while(dataScan.hasNext()){
            station = dataScan.next();
            date = dataScan.next();
            prcp = dataScan.nextInt();
            snow = dataScan.nextInt();
            snwd = dataScan.nextInt();
            tmax = dataScan.nextInt();
            tmin = dataScan.nextInt();
            System.out.printf("%17s %10s %8.1f %8.1f %8.1f %8.1f %8.1f \n", station, date(date), prcp(prcp), inch(snow), inch(snwd), temp(tmax), temp(tmin));
        }
    }

}
public static String date(String theDate){
    String dateData = theDate;
    String a = dateData.substring(4,6);
    String b = dateData.substring(6,8);
    String c = dateData.substring(0,4);
    String finalDate = a + "/" + b + "/" + c;
    return finalDate;

}

public static double prcp(double thePrcp){
    double a = (thePrcp * 0.1) / 25.4;
    return a;
}

public static double inch(double theInch){
    double a = theInch / 25.4;
    if(theInch == 9999){
        a = 9999;
    }
    return a;
}


public static double temp(double theTemp){
    double a = ((0.10 * theTemp) * 9/5 + 32);
    return a;
}
}

我遇到的问题是取值并检查所有时间“9999”出现,并打印出“----”。我不知道如何接受 double 类型的值,并打印出一个字符串。

此代码获取值并检查值 9999,但不对其执行任何操作。这就是我的问题所在:

public static double inch(double theInch){
    double a = theInch / 25.4;
    if(theInch == 9999){
        a = "----";
    }
    return a;
}

如果我在这个问题上提供了很多信息,我很抱歉。如果您需要我澄清,请询问。谢谢你的帮助!

4

2 回答 2

6

您需要修改inch函数以返回字符串,而不是双精度。

public static String inch(double theInch){
    if(theInch == 9999){
        return "----";
    }
    return Double.toString(theInch/25.4);
}
于 2012-11-29T01:52:39.527 回答
2

我认为第一个问题可能是您正在从Scannerasint而不是doubles 中读取所有值。例如,根据您的System.out.println()陈述,我认为您实际上应该阅读以下数据类型...

        prcp = dataScan.nextDouble();
        snow = dataScan.nextDouble();
        snwd = dataScan.nextDouble();
        tmax = dataScan.nextDouble();
        tmin = dataScan.nextDouble();

此外,看起来该方法似乎inch()只会在该System.out.println()行中使用,您需要将其更改为 aString作为返回类型......

public String inch(double theInch){
    if (theInch == 9999){
        return "----";
    }
    return ""+(theInch/25.4);
}
于 2012-11-29T01:55:09.903 回答