1

所以这就是问题所在:我试图让我的代码简单。我正在从一行中包含多个数据项的文件中读取数据,因此我想将它们拆分并放入相应的数组中。但是,IDE 一直告诉我找不到该符号。我已经尝试过导入字符串类(正确的词?)并将我的元素数组移动到 WHILE 循环内。

我究竟做错了什么?

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[64];

String [] hurrcaneYear = new String[64];
String [] hurrcaneName = new String[64];
int []  hurricaneCategory = new int[64];
double [] hurrcanePressure = new double[64];
double tempKnots;
double knotsToMph; 
double [] hurricaneWindSpeeds = 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.print(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.hasNext()) 
{
    token[counter] = in.nextLine();
    String tokenElements[] = token.split(" ");
    counter++;
}
for(String line:token) 
{

}
}

}
4

1 回答 1

7

您只能将split方法应用于对象,而String不是对象。arrayString

我想你想做:

token[counter] = in.nextLine();
String tokenElements[] = token[counter].split(" ");


文档 中的注释:

// this form is discouraged
float anArrayOfFloats[];

公约不鼓励这种形式;括号标识数组类型,并应与类型名称一起出现。

即使它是编译的,我建议将你的数组声明为String [] tokenElements

于 2013-10-31T16:52:29.707 回答