Can someone help me write this program: The code would look something like this,
ID: 3664764 • Letter: C
Question
Can someone help me write this program:
The code would look something like this, called a try-catch block:
In this case, we could go back and retry (maybe after prompting for a new file name) or print a message and quit anyway. Something like:
Write a program OpenFiles.java that reads file names from a keyboard Scanner, and attempts to open each of those files for reading. For each file name, construct and then close a fileScanner with that file name. If the construction is successful, print a message. If it is not successful, thecatch block will be executed, and you should print an appropriate message there.
Explanation / Answer
import java.io.*; //input/output and packge for exception FileNotFoundException
import java.util.Scanner ; //importing scanner class object
import javax.swing.JOptionPane ; //class JOptionPane
/**
Write a program OpenFiles.java that reads file names from a keyboard Scanner, and attempts to open each of those files for reading. For each file name, construct and then close a fileScanner with that file name. If the construction is successful, print a message. If it is not successful, thecatch block will be executed, and you should print an appropriate message there.
*/
public class OpenFile
{
public static void main( String []arguments )
{
File filInput ; //fileInput variable for file input
Scanner input;//importing the file through scanner class variable
String nameOfFile ; //holds the fileName
//Get fileName from user
nameOfFile =JOptionPane.showInputDialog( " PLEASE TYPE " + " a file name : " ) ;
//trys to open the file
try
{
filInput =new File( nameOfFile );
input=new Scanner( filInput );
JOptionPane.showMessageDialog( null, " File is not founded... " ) ;
}
catch ( FileNotFoundException e )
{
JOptionPane.showMessageDialog( null, " File is not founded..." ) ;
}
JOptionPane.showMessageDialog( null, " Completed... " ) ;
System.exit( 0 ) ;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.