Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

String fileName is read from input. The try block opens a file named fileName for output. Three integers are read from input, and each integer

String fileName is read from input. The try block opens a file named fileName for output. Three integers are read from input, and each integer is written into the file. Write the finally block to perform the following tasks, if the output file is open:
Write to the file numValuesRead, followed by " value(s) read" and a newline.
If numValuesRead is greater than zero, write to the file "Last value is: ", followed by chiveLast and a newline.
Close the file. import java.util.Scanner;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class WriteLastChiveFile {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
PrintWriter chiveInWriter = null;
String fileName;
int chiveValue;
int numValuesRead =0;
int chiveLast =0;
fileName = scnr.next();
try {
chiveInWriter = new PrintWriter(new FileOutputStream(fileName));
for (numValuesRead =0; numValuesRead <3; ++numValuesRead){
chiveValue = scnr.nextInt();
chiveInWriter.println(chiveValue);
chiveLast = chiveValue;
}
}
catch (Exception e){
System.out.println("Error!");
}
/* Your code goes here */
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions

Question

what we can upgrade expansion slots to , if anything

Answered: 1 week ago