verbalh
April 19th, 2008, 04:11 PM
I have been trying to write a program that will loop until a user selects a file with a Try - Catch statements to no success. If user hits Cancel then I need NullPointerException or if no file found I need a FileNotFoundException
Here my Instructions:
****************Instructions**************
Write a GUI program that prompts the user to browse and select a file. Then read the selected file using a Scanner object, a line at a time. Copy the file contents and display it in a text area component. If the user did not select a file, then inform the user (using a pop-up window) that a file must be selected and then prompt the user to select a file. Repeat this until the user selects a file. Provide (copy and paste) your Java Source codes.
• If selected file does not exist, then scanner object may throw a FileNotFoundException.
• If user selects “Cancel” option in the file open dialog, the scanner object may throw a NullPointerException
• Think carefully about how to use the try/catch structure in combination with a loop that prompts the user to browse and select a file.
****************Instructions****************
I have this much so far
*****************************
//********************************************************************
// DisplayFile.java Author:
//
//
//********************************************************************
import java.util.Scanner;
import java.io.*;
import javax.swing.*;
public class DisplayFile
{
//-----------------------------------------------------------------
// Opens a file chooser dialog, reads the selected file and
// loads it into a text area.
//-----------------------------------------------------------------
public static void main (String[] args) throws IOException
{
JFrame frame = new JFrame ("Display File");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JTextArea ta = new JTextArea (20, 30);
JFileChooser chooser = new JFileChooser();
int status = chooser.showOpenDialog (null);
******************************************
Please help
Here my Instructions:
****************Instructions**************
Write a GUI program that prompts the user to browse and select a file. Then read the selected file using a Scanner object, a line at a time. Copy the file contents and display it in a text area component. If the user did not select a file, then inform the user (using a pop-up window) that a file must be selected and then prompt the user to select a file. Repeat this until the user selects a file. Provide (copy and paste) your Java Source codes.
• If selected file does not exist, then scanner object may throw a FileNotFoundException.
• If user selects “Cancel” option in the file open dialog, the scanner object may throw a NullPointerException
• Think carefully about how to use the try/catch structure in combination with a loop that prompts the user to browse and select a file.
****************Instructions****************
I have this much so far
*****************************
//********************************************************************
// DisplayFile.java Author:
//
//
//********************************************************************
import java.util.Scanner;
import java.io.*;
import javax.swing.*;
public class DisplayFile
{
//-----------------------------------------------------------------
// Opens a file chooser dialog, reads the selected file and
// loads it into a text area.
//-----------------------------------------------------------------
public static void main (String[] args) throws IOException
{
JFrame frame = new JFrame ("Display File");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JTextArea ta = new JTextArea (20, 30);
JFileChooser chooser = new JFileChooser();
int status = chooser.showOpenDialog (null);
******************************************
Please help