Java Reference
In-Depth Information
Fig. 4.1
Accepting serial fi le input from the user
had a null reference. As of Java 5, we must not attempt to read beyond the end-of-fi le
if we wish to avoid the generation of a NoSuchElementException . Instead, we have
to check ahead to see whether there is more data to be read. This is done by making
use of the Scanner class's hasNext method, which returns a Boolean result indicating
whether or not there is any more data.
import java.util.*;
public class FileTest3
public static void main(String[] args)
throws IOException
int mark, total=0, count=0;
Scanner input =
new Scanner(new File("marks.txt"));
while (input.hasNext())
mark = input.nextInt();
total += mark;
Search WWH ::

Custom Search