【问题标题】:read new data into array of names from input.txt从 input.txt 将新数据读入名称数组
【发布时间】:2014-09-18 03:51:42
【问题描述】:

我在 while 循环中不断收到此错误:java.lang.ArrayIndexOutOfBoundsException; 这是在:将新数据从“Input.txt”中读取到名称数组中,每行一个字符串,并将它们放在一个数组中。文件中字符串的数量必须与数组中的单元格数量相同。

    // Open an input textfile named "Input.txt".
    File f = new File("Input.txt");
    Scanner inputFile = new Scanner(f);
    System.out.println("\nThe array contents:");

    // Read new data into the array of names from "Input.txt" one String per line, and places them in an array. 
    //The number of Strings in the file must be the same as the number of cells in the array.
    String[] input = new String [5];

    int k=0;
    while (inputFile.hasNext())
    {
        String str = inputFile.nextLine();
        input[k]=str;
        k++;
    }
    inputFile.close();




    //Print the new array contents on the screen
    PrintWriter pw = new PrintWriter("Input.txt");
    for(String str : array)
        pw.println(str);
    pw.close();

}

/**
 * Method printOnScreen sends the entire contents of an 
 * array to the Screen using an "enhanced for" loop.
 * 
 * @param array the array of Strings be printed on Screen
 * 
 */
public static void printOnScreen(String[] args)
{
    for(String val : args)
        System.out.println(val);
}

【问题讨论】:

  • 尝试将这些行放在“ArrayList”中,然后在“ArrayList”上使用“toArray()”方法。

标签: java arrays eclipse compiler-errors


【解决方案1】:

您的数组大小似乎很短。如果您不确定数组大小,我建议您使用 ArrayList。

List<String> input = new ArrayList<String>();

int k=0;
while (inputFile.hasNext())
{
    String str = inputFile.nextLine();
    input.add(str);
}
inputFile.close();

【讨论】:

    【解决方案2】:

    您收到ArrayIndexOutOfBoundException,这意味着您的文件有超过 5 行,并且字符串数组的长度仅为 5,因此会引发异常。 如果您想从文件“Input.txt”中读取 5 个名称,则在 k>5

    时中断循环
    while (inputFile.hasNext())
    {   
        if(k>5) break;
        String str = inputFile.nextLine();
        input[k]=str;
        k++;
    }
    

    或者如果你想读取整个文件,那么你需要使用创建时没有定义大小的数组,比如动态数组:ArrayList

    List<String> input = new ArrayList<String>();
    
    while (inputFile.hasNext())
    {
        String str = inputFile.nextLine();
        input.add(str);
    }
    

    【讨论】:

      【解决方案3】:

      工作示例

      public class Test2 {
      
          public static void main(String[] args) throws IOException {
      
              BufferedReader input = new BufferedReader(new FileReader("d:\\myFile.txt"));
      
              String str;
      
              List<String> list = new ArrayList<String>();
              while ((str = input.readLine()) != null) {
                  list.add(str);
              }
      
              String[] stringArr = list.toArray(new String[0]);
      
              System.out.println("\nThe array contents:");
              for (String val : stringArr)
                  System.out.println(val);
      
          }
      
      }
      

      输出

      数组内容:

      stack
      overflow
      is 
      good
      

      文本文件输入 myFile.txt

      stack
      overflow
      is 
      good
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多