【问题标题】:How to create array from text file using Scanner?如何使用扫描仪从文本文件创建数组?
【发布时间】:2015-06-08 09:48:47
【问题描述】:

我刚开始学习 Java,我正在努力完成这个练习。

我已经了解如何使用扫描仪从 txt 文件中提取信息(我认为)(我们只应该更改方法主体)。但是我不确定将信息传输到数组的正确语法。

我意识到它一定很简单,但我似乎无法弄清楚。有人可以在所需的语法和元素方面为我指出正确的方向吗?提前谢谢!

import java.util.Scanner;
import java.io.FileReader;
import java.io.IOException;

public class Lab02Task2 {

    /**
     * Loads the game records from a text file.
     * A GameRecord array is constructed to store all the game records.
     * The size of the GameRecord array should be the same as the number of non-empty records in the text file.
     * The GameRecord array contains no null/empty entries.
     * 
     * @param reader    The java.io.Reader object that points to the text file to be read.
     * @return  A GameRecord array containing all the game records read from the text file.
     */
    public GameRecord[] loadGameRecord(java.io.Reader reader) {

        // write your code after this line

        Scanner input = new Scanner(reader);
        for (int i=0; input.hasNextLine(); i++) {
            String inputRecord = input.nextLine();
            input = new Scanner(inputRecord);
            // array?
        }
        return null; // this line should be modified/removed after finishing the implementation of this method.
    }
}

【问题讨论】:

    标签: java arrays syntax java.util.scanner


    【解决方案1】:

    如果你已经有一个String的文件内容,你可以说:

    String[] words = content.split("\\s");
    

    【讨论】:

      【解决方案2】:

      你可以像这样解析你的字符串:

      private ArrayList<String> parse(BufferedReader input) throws CsvException {
          ArrayList<String> data = new ArrayList<>();
      
          final String recordDelimiter = "\\r?\\n|\\r";
          final String fieldDelimiter = "\\t";
      
          Scanner scanner = new Scanner(input);
          scanner.useDelimiter(recordDelimiter);
      
           while( scanner.hasNext() ) {
              String line = scanner.next();
              data.add(line);
           }
      
           return data;
      }
      

      输入的文本将被逐行扫描。

      【讨论】:

        【解决方案3】:

        您可以使用ArrayList&lt;String&gt;,如下所示:

        Scanner s = new Scanner(new File(//Here the path of your file));
        
        ArrayList<String> list = new ArrayList<String>();
        
        while (s.hasNext())
        {
            list.add(s.nextLine());
        }
        

        如果您想获取ArrayList 的某些项目的值,您只需使用 get 函数进行引用,如下所示:

        list.get(//Here the position of the value in the ArrayList);
        

        所以,如果你想获取ArrayList 的所有值,你可以使用循环来完成:

        for (int i = 0; i < list.size(); i++)
        {
           System.out.println(list.get(i));
        }
        

        最后关闭你的Scanner

        s.close();
        

        希望对你有帮助!

        【讨论】:

        • 链表不会更快吗?
        • @DanielM。哦,我不知道链表的概念,所以我不能说什么会更快。谢谢你给我的新概念!
        【解决方案4】:

        假设文件中的一行只包含一个游戏

         for (int i=0; input.hasNextLine(); i++) {
                       String inputRecord = input.nextLine();
                       input = new Scanner(inputRecord);
        
                       String line=input.nextLine();
        
                       arr[i]=line;
                     }  
            return arr;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-13
          • 1970-01-01
          • 2013-08-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多