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!
The
mainmethod must bestatic:Edit – onto solving the real problem
The loop only runs once because, after the first pass through the
whilebody,iwill be equal to1. At that pointline[1]is null, because you haven’t read anything into it. Here’s the typical idiom used instead (note the changes in variable names):