Page 676 - Introduction to Programming with Java: A Problem Solving Approach
P. 676
642
Chapter 15 Files
SunnyDale milk - in stock: 2, price: $2.50
eggs - in stock: 2, price: $1.50
BrookSide milk - in stock: 4, price: $1.95
Total value: $165.65
bread - in stock: 15, price: $9.99
SunnyDale milk - in stock: 2, price: $2.00
eggs - in stock: 3, price: $1.50
Total value: $158.35
11. [after §15.9] Suppose you used a program like Microsoft’s Notepad or UNIX’s vi to create a text file, alphabet.txt, that contains this single line of text: “abcdefg.” Provide the missing code fragments in the following Java program so that it successfully reads and displays the data in that file.
/*************************************************************
*
TextReader.java
Dean & Dean
This reads a line of text from a text file.
*************************************************************/
*
*
*
<fragment>
public class TextReader
{
}
// end TextReader class
{
}
// end main
Apago PDF Enhancer
File file = <fragment>; Scanner fileIn;
try
{
public static void main(String[] args)
}
fileIn = <fragment>; System.out.println(fileIn.nextLine());
<fragment>
catch (FileNotFoundException e)
}
// close the file
System.out.println(e.getMessage());
{
12. [after §15.9] Suppose a text file called myDates.txt contains this line of text: 1999 2000 2001 2002
Modify the program in the previous exercise so that it reads the data from this file as integers and immediately prints them out onto the screen, like this:
Output:
1999
2000