0

虽然我已经解决了我之前遇到的 split() 问题(感谢最后一个帮助我的人!)我还有另一个问题。这在此处列出的代码中很明显,但是我不知道它是什么。问题是它只将文件的最后一行打印到输出中,而在这个阶段,我希望它打印出所有行。我需要解决什么问题?谢谢!!

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 [] 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.hasNextLine()) 
{
    token[counter] = inFile.nextLine();
    tokenElements = token[counter].split("  ");
    counter++;
}
int counter2 = 0;
for(String line:tokenElements) 
{
System.out.println(tokenElements[counter2]);
counter2++;
}
}
4

3 回答 3

2

在“while 循环”中移动“for 循环”。

    while(inFile.hasNextLine()){
        token[counter] = inFile.nextLine();
        tokenElements = token[counter].split("  ");
        counter++;

        for(String part:tokenElements){
            System.out.println(part);
        }
   }
于 2013-11-01T15:54:11.777 回答
0

在那个循环中:

while(inFile.hasNextLine()) 
{
    token[counter] = inFile.nextLine();
    tokenElements = token[counter].split("  ");
    counter++;
}

您将替换tokenElements为读取的最后一行的元素。然后,如果您似乎认为后面的“for 循环”tokenElements包含每一行。不是这种情况。您应该将最后一行添加到列表中,然后打印该列表。

所以一开始你定义:

List<String[]> lines = new ArrayList<>();

然后你用这种方式修改你的“while循环”:

while(inFile.hasNextLine()) 
{
    token[counter] = inFile.nextLine();
    lines.add(token[counter].split("  "));
    counter++;
}

然后你可以打印所有的行:

for (String[] tokenElements : lines) {
     for (String token : tokenElements) {
          System.out.print(token + " ");
     }
     System.out.println();
}

通常,您应该使用自动增长的列表,而不是具有诸如此类的神奇值的数组String[128]

于 2013-11-01T16:00:13.217 回答
0

当您使用 foreach 时,您不必使用计数器:

for(String line : tokenElements) {
   System.out.println(line);         
}

话虽如此,您在 while 循环中用最新的 tokenElements 覆盖,因此仅打印出最后一行,因为您的 tokenElements 仅包含一行的单词。

您实际上可以跳过“拆分”部分,并跳过第二个计数器;-):

while (inFile.hasNextLine()) {
    System.out.println(inFile.nextLine());
}
于 2013-11-01T16:09:28.117 回答