1

I read in the contents of my File likewise:

List<String> list = new ArrayList();
Scanner scanner = new Scanner(new FileInputStream(file));
while( scanner.hasNextLine())
{
  list.add(scanner.nextLine());
}

At the EOF I want to send the String "@@@" to act as a Sentinel Value to know that its the end. However, I do not have "@@@" in the File that is being read into. Any suggestions on how I might approach this?

durron597
  • 31,968
  • 17
  • 99
  • 158
Beginner
  • 143
  • 2
  • 4
  • 11
  • If your storing the lines in a List, why do want the sentinel value? The last element of the List is the EOF. – Shar1er80 Apr 17 '15 at 18:08

2 Answers2

1
List<String> list = new ArrayList();
Scanner scanner = new Scanner(new FileInputStream(file));
while( scanner.hasNextLine())
{
    list.add(scanner.nextLine());
}
list.add("@@@");
jah
  • 1,265
  • 10
  • 21
0

Just add the line list.add("@@@"); after your while loop.

You'll know you've read the entire file in by then, so just add the constant string. You may want to consider separating out the string for readability with something like:

public static final String SENTINEL = "@@@";
durron597
  • 31,968
  • 17
  • 99
  • 158