Given a Scanner reference variable, stdin, that is associated with standard inpu
ID: 3815116 • Letter: G
Question
Given a Scanner reference variable, stdin, that is associated with standard input, read in two words. Use the first word as the name of a file to create and store the second word in. Make sure that the data written to the file has been flushed from its buffer and that any system resources used during the course of running these statements have been released. (Do not concern yourself with any possible exceptions here ---assume they are handled elsewhere.) Scanner stdin = new Scanner {stdin. flush (); stdin.close ();Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner stdin = new Scanner(System.in);
String name = stdin.next();
String store = stdin.next();
PrintWriter pw = new PrintWriter(new File(name));
pw.write(store);
pw.flush();
pw.close();
stdin.close();
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.