我一直在开发一个程序来从文件中收集数据并对其进行一些处理(如代码和伪代码中所示),但是我很难将字符串的每个元素放入正确的数组中。文件中的一行如下所示:
1980 Aug 945 100 Allen
我想使用 substring 方法,因为这在我看来是最简单的。有更好的方法吗?到目前为止,这是我的代码。问题究竟出在哪里,我应该如何解决?谢谢!:)
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.lang.String;
public class Hurricanes2
{
public static void main(String [] args) throws IOException
{
int counter = 0;
String [] token = new String[1000];
String [] tokenElements = new String[128];
String [] hurricaneYear = new String[64];
String [] hurricaneName = new String[64];
String [] hurricaneMonth = new String[64];
int [] hurricaneCategory = new int[64];
double [] hurricanePressure = new double[64];
double tempKnots;
double knotsToMph;
double [] hurricaneWindSpeed = new double[64];
double categoryAverage;
double pressureAverage;
double speedAverage;
String headerData = " Hurricanes 1980 - 2006\n\n Year Hurricane Category Pressure(MB) Wind Speed (MPH)\n========================================================================";
Scanner in = new Scanner(System.in);
Scanner inFile = new Scanner(new File("hurcData2.txt"));
System.out.println(headerData);
/**---Use for-each (line:token)
* Parse for year - > year array
* parse for name - > name array
* parse for knots - > tempKnots
* knotsToMph = tempKnots * 1.15078
* hurricaneWindSpeed[counter] = knotsToMph
* enter if-else to calculate category (hurricaneCategory [] = 1,2,3,4, or 5):
* 74-95 cat1
* 96-110 cat2
* 111 - 129 cat3
* 130-156 cat4
* 157 or higher cat 5
*
*
*/
while(inFile.hasNextLine()){
token[counter] = inFile.nextLine();
String tempToken = token[counter];
hurricaneYear[counter] = tempToken.substring(0, 3);
hurricaneMonth[counter] = tempToken.substring(6, 8);
hurricanePressure[counter] = Double.parseDouble(tempToken.substring(10, 12));
hurricaneWindSpeed[counter] = Double.parseDouble(tempToken.substring(14, 16));
hurricaneName[counter] = tempToken.substring(17);
counter++;
}
System.out.print("Lines: " + counter);
}
}