java中读取和替换行

发布于 2024-10-20 23:35:13 字数 1096 浏览 6 评论 0原文

我试图将 file.txt 逐行读入 java,然后当一行是“foo”时,我将其后面的行设置为“lineAfterFoo”,然后将其输出给用户。

我的 Java 代码...

public void main(String[] args) throws IOException {

    try {
        FileReader someFile = new FileReader("file.txt");
        BufferedReader input = new BufferedReader(someFile);
        int i = 0;
        String[] line;
        line = new String[10];
        line[i] = input.readLine();

            while(line[i] != null) {

                line[i] = input.readLine();

                if (line[i] == "foo") {
                    i = i + 1;

                    line[i] = "lineAfterFoo";
                }

                i = i + 1;

            }

            for (int number = 1; number < i; number++) {
                System.out.println(line[number]);
            }

    } catch (FileNotFoundException e) {
        e.printStackTrace();

    }

}

File.txt

1
2
3
foo
HopeFullyThisWillChange
5
6
7
8
9
10

错误...

java.lang.NoSuchMethodError: main
Exception in thread "main" 

感谢您的帮助!

I'm attempting to read the file.txt into java line by line and then when a line is "foo" I set the line after it to be "lineAfterFoo" then output that to the user.

My Java Code....

public void main(String[] args) throws IOException {

    try {
        FileReader someFile = new FileReader("file.txt");
        BufferedReader input = new BufferedReader(someFile);
        int i = 0;
        String[] line;
        line = new String[10];
        line[i] = input.readLine();

            while(line[i] != null) {

                line[i] = input.readLine();

                if (line[i] == "foo") {
                    i = i + 1;

                    line[i] = "lineAfterFoo";
                }

                i = i + 1;

            }

            for (int number = 1; number < i; number++) {
                System.out.println(line[number]);
            }

    } catch (FileNotFoundException e) {
        e.printStackTrace();

    }

}

File.txt

1
2
3
foo
HopeFullyThisWillChange
5
6
7
8
9
10

The Error...

java.lang.NoSuchMethodError: main
Exception in thread "main" 

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

箹锭⒈辈孓 2024-10-27 23:35:13

main 方法必须是static

public static void main(String[] args) throws IOException {
    // snip...  
}

编辑 - 解决实际问题

循环只运行一次,因为在第一次通过 while 主体之后, i 将等于 1。此时 line[1] 为 null,因为您还没有读入任何内容。这是所使用的典型习惯用法(注意变量名称的变化):

int i = 0;
String line = null;
String[] lines = new String[10];

// read the next line and immediately check to see if it's null
// also make sure that i doesn't go out of range
while ((line = input.readLine()) != null
    && i < lines.length) {
    lines[i] = line;

    // Use .equals() (not ==) when comparing strings!
    if ("foo".equals(line)) {
        i++; // shorter form of i=i+1
        lines[i] = "lineAfterFoo";
    }
    i++;
}

The main method must be static:

public static void main(String[] args) throws IOException {
    // snip...  
}

Edit - onto solving the real problem

The loop only runs once because, after the first pass through the while body, i will be equal to 1. At that point line[1] is null, because you haven't read anything into it. Here's the typical idiom used instead (note the changes in variable names):

int i = 0;
String line = null;
String[] lines = new String[10];

// read the next line and immediately check to see if it's null
// also make sure that i doesn't go out of range
while ((line = input.readLine()) != null
    && i < lines.length) {
    lines[i] = line;

    // Use .equals() (not ==) when comparing strings!
    if ("foo".equals(line)) {
        i++; // shorter form of i=i+1
        lines[i] = "lineAfterFoo";
    }
    i++;
}
青巷忧颜 2024-10-27 23:35:13

该错误与您的代码根本无关,您只是试图执行错误的类。检查您的 IDE 配置,并使用 java MyMainClass 在命令行上进行测试。

This error is not related to your code at all, you're simply trying to execute the wrong class. Check your IDE configuration, and test on the command line with java MyMainClass.

墨小墨 2024-10-27 23:35:13

main 不需要是 static 吗?

Doesn't main need to be static?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文