How would I print the elements in this case, words from a file. My program currently takes input from a file called input with contains the words "one two three four galumph" just like that. As of right now, it list each word on a separate line and once it reaches "galumph" it terminates, which is what I want, but I would also like for it to displays all the elements in the file before anything. How would I do that? I tried doing a while loop at the very beginning, but get errors. Any suggestions?
import java.io.*;
import java.util.Scanner;
class EchoWords {
public static void main(String[] args) throws FileNotFoundException {
String word;
String line;
try {
File file = new File("input");
Scanner s2 = new Scanner(file);
while(s2.hasNext()) {
String s = s2.next();
System.out.println(s);
while(true) {
word = s2.next();
if(word.equals("galumph")) {
break;
}else{
System.out.println(word);
continue;
}
}
System.out.println("bye!");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}