0
import java.io.File;

import java.io.FileNotFoundException; import java.util.Scanner;

public class MailMessage {

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    int i = 0;
    String condition = "EOF";
    String[] mail = new String[500];
    File file = new File("C:\\Users\\sailo\\eclipse-workspace\\mail.txt");
    Scanner mailInput = new Scanner(file);
    while (mailInput.hasNextLine() && i < 500 && condition != "EOF") {
        mail[i] = mailInput.nextLine();
        i++;
        System.out.print(mail[1] + "\n");
    }
}

} This is what the txt file looks like

  • 4
    `condition != "EOF"` is not the proper way to compare Strings in Java. Also will not `hasNextLine` already take care of the end of file? Also `condition ` is assigned the value `EOF` and it never changes. – Scary Wombat Sep 21 '21 at 02:17
  • @ScaryWombat Ah thank you! It's working now as each indices of the array contains a line. Is there any way I can store the whole paragraph before EOF into each indices though? – Nam Ngo Ngoc Sep 21 '21 at 02:23
  • 1
    Yeah, try coding it yourself and then come back here if you have trouble. – Scary Wombat Sep 21 '21 at 02:24
  • Or ... just read https://stackoverflow.com/questions/285712 – Stephen C Sep 21 '21 at 14:43

0 Answers0