简体   繁体   English

如何将同一文本文件中的整数和字符串都添加到不同的数组中

[英]How to add both ints and strings from the same text file into different arrays Java

I need to make a program that stores values from a text file into multiple arrays. 我需要制作一个程序,将文本文件中的值存储到多个数组中。 The text file looks like this: 文本文件如下所示:

1995 Jun 987 65 Allison 1995年6月987 65艾莉森

1995 Jul 973 85 Erin 1995年7月973 85艾琳

1995 Aug 929 120 Felix 1995年8月929120费利克斯

1995 Aug 968 95 Humberto 1995年8月968 95温贝托

1995 Aug 965 95 Iris 1995年8月965 95 Iris

I need to add each column into a separate array. 我需要将每列添加到单独的数组中。 How do I tell the program to specifically target one column to add it to the array, then the next? 我如何告诉程序专门针对一列将其添加到数组,然后添加到另一列?

I tried to just do it like this: 我试图这样做:

    File fileName = new File ("hurricanedata.txt");
    Scanner inFile = new Scanner(fileName);


    while (inFile.hasNextLine()) {
        index++;
        inFile.nextLine();
    }

    hurricaneNames = new String [index];
    years = new int [index];
    months = new String [index];
    pressures = new int [index];
    windSpeeds = new int [index];

    while (inFile.hasNext()) {

        int i = 0;
        years [i] = inFile.nextInt();
        months [i] = inFile.next();
        pressures [i] = inFile.nextInt();
        windSpeeds [i] = inFile.nextInt();
        hurricaneNames [i] = inFile.next();
        System.out.println(years[i]);  //print statement to test
        i++;
    }

But the print statement doesn't print anything. 但是print语句不打印任何内容。

After you do this: 完成此操作后:

while (inFile.hasNextLine()) {
    index++;
    inFile.nextLine(); // HERE
}

your file is now being read at the end of the file. 您的文件现在正在文件末尾读取。 So next() won't start at the beginning of the file like you want it to. 因此,next()不会像您希望的那样从文件的开头开始。

You could simply reopen the file with the scanner: 您只需使用扫描仪重新打开文件即可:

File fileName = new File ("hurricanedata.txt");
Scanner inFile = new Scanner(fileName);


while (inFile.hasNextLine()) {
    index++;
    inFile.nextLine(); // Increments the line of the file that you're reading, so the
                       // next "next()" you call starts at the >next< line of the file.
}
inFile.close();
inFile = new Scanner(fileName); // NOTE: Restarts the Scanner, ensuring that it
                                // starts reading from the beginning of the file again.

hurricaneNames = new String [index];
years = new int [index];
months = new String [index];
pressures = new int [index];
windSpeeds = new int [index];

int i = 0; // NOTE: This should be OUT of the loop, so that i increments
           // and doesn't restart to 0.
while (inFile.hasNext()) {

    years [i] = inFile.nextInt();
    months [i] = inFile.next();
    pressures [i] = inFile.nextInt();
    windSpeeds [i] = inFile.nextInt();
    hurricaneNames [i] = inFile.next();
    System.out.println(years[i]);  //print statement to test
    i++;
}

In the first while loop, your read the till the end of file . 在第一个while循环中,您读到file末尾 So, in second while loop, you have nothing to read as the pointer is on end of file. 因此,在第二个while循环中,由于指针位于文件末尾,因此您无需读取任何内容。

You can do this in one loop only like below: 您只能在一个循环中执行此操作,如下所示:

File fileName = new File ("hurricanedata.txt");
Scanner inFile = new Scanner(fileName);

List<String> hurricaneNames = new ArrayList<>();
List<Integer> years = new ArrayList<>();
List<String> months = new ArrayList<>();
List<Integer> pressures = new ArrayList<>();
List<Integer> windSpeeds = new ArrayList<>();

while (inFile.hasNextLine()) {
    String[] tempArray = inFile.nextLine().split(" ");
    years.add(Integer.parseInt(tempArray[0]));
    months.add(tempArray[1]);
    pressures.add(Integer.parseInt(tempArray[2]));
    windSpeeds.add(Integer.parseInt(tempArray[3]));
    hurricaneNames.add(tempArray[4]);
}

In your first loop you tested inFile.nextLine() . 在第一个循环中,您测试了inFile.nextLine() So, when you're using your second loop inFile.nextLine() is exhausted. 因此,当您使用第二个循环时, inFile.nextLine()已耗尽。

Your first loop just counts the lines in the file. 您的第一个循环仅计算文件中的行数。 Then your second loop reads the data, but by then the file is at the end so there is nothing to read. 然后,您的第二个循环读取数据,但是到那时该文件位于末尾,因此没有要读取的内容。

Let me make a couple of suggestions. 让我提出一些建议。 First, calling the number of lines index is misleading. 首先,调用行数index会产生误导。 That variable is never used as an index, but only as an array size. 该变量从不用作索引,而仅用作数组大小。 Call it lineCount instead. lineCount

Secondly, you can get the number of lines in the file with much simpler code using Java NIO and Streams. 其次,您可以使用Java NIO和Streams使用更简单的代码来获取文件中的行数。

The rest of your code can stay mostly the same. 您的其余代码大部分都可以保持不变。 The index variable i needs to be outside the while loop. 索引变量i需要在while循环之外。 And preferably wrap the Scanner in a try-with-resources loop. 并且最好将Scanner包装在try-with-resources循环中。

final Path filePath = Paths.get("hurricanedata.txt");
final int lineCount;

try (Stream<String> lines = Files.lines(filePath)) {
    lineCount = (int)lines.count();
}

hurricaneNames = new String[lineCount];
years = new int[lineCount];
months = new String[lineCount];
pressures = new int[lineCount];
windSpeeds = new int[lineCount];

try (Scanner inFile = new Scanner(filePath)) {
    int i = 0;
    while (inFile.hasNext()) {
        years[i] = inFile.nextInt();
        months[i] = inFile.next();
        pressures[i] = inFile.nextInt();
        windSpeeds[i] = inFile.nextInt();
        hurricaneNames[i] = inFile.next();
        System.out.println(years[i]);  //print statement to test
        i++;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从单个文本文件中同时打印字符串和整数? - How do I print both Strings and Ints from a single text file? 如何在java中将不同数组的字符串添加到一个数组中 - how to add strings from different arrays into one array in java 读取文本文件中的每一行以获取字符串,双精度和整数,并将它们放置在不同的数组中 - Read each line in a text file for strings, doubles, and ints an place them in different arrays 如何从字符串和整数的文本文件中提取整数值? - How to pull int value from text file of strings and ints? Java:如何从文本文件中删除具有相同前缀的字符串? - Java: How to remove strings having the same prefix from a text file? 在从文本文件读取字符串行之前,在Java中使用BufferedReader读取一行int - Using BufferedReader in java to read a line of ints before reading lines of Strings from a text file 将int从文本文件解析为Java中的构造函数 - Parsing ints from text file into constructor in java 输入磁盘文件并将其存储到字符串和整数中 - inputting a disk file and storing it into both strings and ints 从Java中的字符串中提取整数 - Extracting Ints from Strings in Java 扫描仪 - 从文本文件中读取字符串、整数、双精度数 - Scanner - Reading Strings, Ints, Doubles from a text File
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM