【发布时间】:2015-04-02 19:18:23
【问题描述】:
我正在尝试从 csv 文件中的不同列创建两个不同的数组。 csv 文件有一列用于“月份”,一列用于“温度”。这是我目前所拥有的,但我不知道如何将这些值转换为数组。
public class InputOutput
{
private double temperature;
public InputOutput()
{
List<String> temperatures = new ArrayList<String>();
Iterator<String> tempIterator = temperatures.iterator();
}
public static double getTemp()
{
double tempReading = 0.0;
try
{
String fileName = "C:\\Users\\Joseph\\Downloads\\MNMINPLS.csv"; //location of MNMINPLS.csv
File file = new File(fileName);
Scanner scan = new Scanner(file); //scanner reads file
while(scan.hasNext()) //iterator
{
String tempData = scan.next();
String[] nums = tempData.split(",");
tempReading = Double.parseDouble(nums[3]);
System.out.println(tempReading);
}//end while
scan.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}//end try/catch
return tempReading;
}//end method getTemp
public static int getMonth()
{
int m = 0;
try
{
String fileName = "C:\\Users\\Joseph\\Downloads\\MNMINPLS.csv"; //location of MNMINPLS.csv
File file = new File(fileName);
Scanner scan = new Scanner(file); //scanner reads file
while(scan.hasNext()) //iterator
{
String month = scan.next();
if(month == month)
m++;
String[] timeOfYear = month.split(",");
m = Integer.parseInt(timeOfYear[0]);
System.out.println(m);
}
scan.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}//end try/catch
return m;
}
}
【问题讨论】:
-
您想要数组或数组列表(因为您有一个温度数组列表)?每当您不知道如何使用标准 Java 类时,最好先咨询documentation。您会发现这样做之后,您大部分时间都可以回答自己的问题。提示:ArrayList 有一个 .add() 方法。