How do you create a Scanner to read data from a file? What is the reason to define throws Exception in the main method in Listing 12.15, ReadData.java? What would happen if the close() method were not invoked in Listing 12.15?
Added by Rebecca O.
Step 1
You will typically import `java.io.File` and `java.util.Scanner`. Then, you can create a `File` object that points to the file you want to read, and pass that `File` object to the `Scanner` constructor. For example: ```java File file = new Show more…
Show all steps
Your feedback will help us improve your experience
Akash M and 96 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
9.12.5: Arrange the code to compute the sum of all integers. In the code below, the sum method computes the sum of all integers in a file. The main method calls it with the name of a file and prints the result. Rearrange the lines of code. (Hint: Where should the checked FileNotFoundException be handled?)
Akash M.
Create a package named "reading_with_exceptions". Write a class named ReadingWithExceptions with the following method: void process(String inputFilename) Your program will encounter errors, and we want you to gracefully handle them in such a way that you print out informative information and continue executing. 1. Your process routine will try to open a file with the name of inputFilename for input. If there is any problem (i.e. the file doesn't exist), then you should catch the exception, give an appropriate error, and then return. Otherwise, your program reads the file for instructions. 2. Your process routine will read the first line of the file looking for an outputFilename String followed by an integer. i.e.: outputFilename number_to_read. Your program will want to write output to a file having the name outputFilename. Your program will try to read from "inputFilename" the number of integers found in "number_to_read". 3. Your process method will copy the integers read from inputFilename and write them to your output file (i.e. outputFilename). There should be 10 numbers per line of output in your output file. 4. If you encounter bad input, your program should not die with an exception. For example: - If the count of the numbers to be read is bad or < 0, you will print out a complaint message and then read as many integers as you find. - If any of the other numbers are bad, print a complaint message and skip over the data. - If you don't have enough input numbers, complain but do not abort. 5. After you have processed inputFilename, I would like your program to then close the output file and tell the user that the file is created. Then open up the output file and copy it to the screen. For example, if inputFilename contained: MyOutput.txt 23 20 1 4 5 7 45 1 2 3 4 5 6 7 8 9 77 88 99 23 34 56 66 77 88 99 100 110 120 We would expect the output of your program to be (Note that after 23 numbers we stop printing numbers): MyOutput.txt created with the following output: 20 1 4 5 7 45 1 2 3 4 5 6 7 8 9 77 88 99 23 34 56 66 77 88 99 100 110 120 The main program will access the command line to obtain the list of filenames to call your process routine with.
Supreeta N.
import java.util.Scanner; import java.io.FileInputStream; import java.io.IOException; public class LabProgram { public static void main(String[] args) throws IOException { Scanner scnr = new Scanner(System.in); /* Type your code here. */ } } 6.18 LAB: Parsing food data Given a text file containing the availability of food items, write a program that reads the information from the text file and outputs the available food items. The program first reads the name of the text file from the user. The program then reads the text file, stores the information into four separate arrays, and outputs the available food items in the following format: name (category) -- description Assume the text file contains the category, name, description, and availability of at least one food item, separated by a tab character. Ex: If the input of the program is: food.txt
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
Watch the video solution with this free unlock.
EMAIL
PASSWORD