【问题标题】:why won't my for loop work to print out value in the text file?为什么我的 for 循环不能在文本文件中打印出值?
【发布时间】:2016-03-02 01:31:17
【问题描述】:

我知道这很简单,但由于某种原因,我无法让这个循环工作。非常感谢您的帮助

public Instructor()
    {
        // initialise instance variables
        writeMap = new HashMap<String,String>();
        notRecognised = new ArrayList<String>();
        help = new FileHelper();
        notRecognised = help.readAList("missed.txt");
        for(String i : notRecognised.length) {
            System.out.println(i);

        }
    }

【问题讨论】:

  • 您是否希望notRecognised.length 返回String

标签: java arraylist bluej


【解决方案1】:

您的增强 for 循环不正确。使用列表,而不是长度

for (String i : notRecognised) {
    System.out.println(i);
}

【讨论】:

    【解决方案2】:

    你会想要迭代一个集合,而不是一个长度:

        for ( String i : notRecognized )
    

    请参阅Java for tutorial 中的底部示例。

    【讨论】:

      【解决方案3】:

      可能missed.txt 为空,因此help.readAList() 函数返回空字符串。

       notRecognised = help.readAList("missed.txt");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-20
        • 2020-09-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多