0

项目分配如下:

编写一个名为 FileNerd 的类,该类将从存储在 temp_Larry 文件夹中的名为 NerdData 的文件中输入文本行(假设您的名字是 Larry)。在文件输入循环之后,创建一个循环,在其中只打印以单词“The”开头的那些行。

所以这就是我到目前为止

import java.util.*;
import java.io.*;
public class FileNerd
{
    public static void main (String args[]) throws IOException
    {
        Scanner sf = new Scanner(new File("C:\\temp_Larry\\NerdData.txt"));
        int maxIndx = -1;
        String text[] = new String[100 ];
        while(sf.hasNext())
        {
            maxIndx++;
            text[maxIndx]=sf.nextLine();
        }
        sf.close();

        for(int j = 0; j <= maxIndx; j++)
        {
            String q = text[j];

            if( q.substring(3).equals("The"))
            {
                System.out.println(q);
            }
        }
    }
}

我不确定我做错了什么,因为它编译时没有语法错误但没有打印。

4

1 回答 1

0

关于调试的一些想法:

  • 您可以在整个代码中编写附加System.out.println(debug_msg)语句以了解发生了什么。
  • 当您添加这些调试行时,您通常会发现如何解决您自己的问题。
  • 此外,您可以使用调试器(大多数 IDE 都附带一个)。您可以设置断点来停止程序并遍历代码。

查看您的代码,我没有看到任何明显的错误。我建议添加此调试行,以便您了解发生了什么并自行调试。

for(int j = 0; j <= maxIndx; j++) {
     String q = text[j];
     // this will tell you what the contents of the line are
     System.out.println(q)

     if( q.substring(3).equals("The")) {
        System.out.println(q);
     }
}
于 2014-10-25T18:16:18.477 回答