views:

203

answers:

3

Hey everyone, I'm working on a hex dump utility made in Java. I'm having some issues with the Scanner I'd be using before the hex/ascii logic.

Right now I have the logic and a few other things excluded for debugging reasons, but if anyone knows what's going on I'd like to know! Hints greatly appreciated.

package filedumputility;

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class FileDump extends JFrame {

public FileDump() throws Exception{
    setTitle("File Dump Utility");
    add(new DumpPanel());
}

//Main method
public static void main(String[] args) throws Exception {
    FileDump frame = new FileDump();
    frame.setSize(500, 250);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setVisible(true);
}
//Class to creaste components
class DumpPanel extends JPanel {
    private JFileChooser fileChooser = new JFileChooser();
    private JTextArea dumpArea = new JTextArea(11, 20);
    private JTextField fileName = new JTextField(16);
    private JButton newButton = new JButton("New File");
    private JButton clearButton = new JButton("Clear");
    private JButton dumpButton = new JButton("Dump File");
    private JScrollPane scrollPane = new JScrollPane(dumpArea);
    private JPanel p = new JPanel();
    private Scanner input;

    //Class to add components and read the file
    public DumpPanel() throws Exception {
        p.setLayout(new FlowLayout());
        setLayout(new BorderLayout());

        add(p, BorderLayout.NORTH);
        p.add(fileName);
        p.add(dumpButton);
        p.add(newButton);
        p.add(clearButton);
        add(scrollPane, BorderLayout.SOUTH);

        dumpArea.setEditable(false);
        fileName.setEditable(false);

        //Create event for the new file button
        newButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){                    

                if(fileChooser.showOpenDialog(null)
                        == JFileChooser.APPROVE_OPTION) {

                    //Get the file
                    java.io.File file = fileChooser.getSelectedFile();

                //Set file name bar to the selected file
                fileName.setText(file.getName());
                }
            }
        });

        dumpButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(fileChooser.showOpenDialog(null)
                        == JFileChooser.APPROVE_OPTION) {

                    //Get the file
                    java.io.File file = fileChooser.getSelectedFile();
                    //Create scanner
                    Scanner input = new Scanner(file);

                }
            }
        });

        clearButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                dumpArea.setText(" ");
                fileName.setText(" ");
            }
        });
    }
}
}
A: 

Forgot to add in: The error appears on the Scanner declaration located in the dumpButton ActionListener class.

Bryan
Please edit your question rather than posting again; this isn't a traditional forum.
Michael Myers
+1  A: 

To get your example to compile, you can wrap the new Scanner line in a try catch like this:

            Scanner input = null;
            try {
                input = new Scanner(file);
            } catch(FileNotFoundException ex) {
                // handle this condition appropriately
            }

Once you do that, then you can proceed to processing the lines of the file the way you intended.

Amir Afghani
Thanks for your help!
Bryan
A: 

Your only problem that I can see is that the Scanner constructor throws FileNotFoundException; therefore, you need to enclose it in a try/catch block.

It should look something like this (just for starters):

dumpButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {

            //Get the file
            java.io.File file = fileChooser.getSelectedFile();
            //Create scanner
            try {
                Scanner input = new Scanner(file);
            } catch (FileNotFoundException ex) {
                System.err.println("Error opening " + file.getName());
            }
        }
    }
});

You might also find the Java Exceptions Tutorial helpful.

Michael Myers
Awesome, thank you for your help.
Bryan