Will someone please tell me why I keep getting my custom file not found exception? The file is spelled correctly and is in the same directory of my java file. I am programming on Eclipse on Mac OS X. Here is my source:

// Dillon Sheffield
// Lab 10a
// April 12, 2012

import javax.swing.*; // Needed for Swing classes
import java.util.ArrayList; // Needed for the Array List Object.
import java.awt.*; // Needed for Layout manager and Color
import java.awt.event.*; // Needed for ActionListener Interface
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ShoppingCartSystem extends JFrame
{
private JPanel leftPanel;
private JPanel rightPanel;
private JLabel leftMessageLabel;
private JLabel rightMessageLabel;
private JList bookSelections;
private JList shoppingCartItems;
private ArrayList<String> bookList = new ArrayList<String>();
private JTextField totalTextField;
private JButton removeItemButton;
private JButton clearItemsButton;
private JButton checkoutButton;

final int WINDOW_WIDTH = 750;
final int WINDOW_HEIGHT = 550;

/**
 * Constructor
 */

public ShoppingCartSystem()
{   
    // Set the window's title.
    setTitle("Shopping Cart System");

    // Set the size of this window.
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

    // Specify what happens when the close button is clicked.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Add a FlowLayout manager to the content pane.
    // 0 rows, 2 columns.
    setLayout(new GridLayout(0, 2));

    // Build the panels and add them to the frame.
    buildPanels();

    // Add the panels to the frame's content pane.
    add(leftPanel);
    add(rightPanel);

    // Display the window.
    setVisible(true);
}

/**
 * The buildPanel method adds two labels, text fields, and
 * a button to a panel.
 */

private void buildPanels()
{
    // Create a label to display information.
    leftMessageLabel = 
            new JLabel("Select books that you would like to buy.");

    rightMessageLabel =
            new JLabel("Here are the books that you selected.");

    // Create the lists.
    bookSelections = new JList();
    shoppingCartItems = new JList();

    // Call the PopulateList method to get the book titles and prices.
    populateList();

    // Create a textField 10 characters wide.
    totalTextField = new JTextField(10);

    // BUTTONS.
    removeItemButton = new JButton("Remove");
    clearItemsButton = new JButton("Clear");
    checkoutButton = new JButton("Checkout");

    // EVENT LISTENERS.
    removeItemButton.addActionListener(new ButtonListener());
    clearItemsButton.addActionListener(new ButtonListener());
    checkoutButton.addActionListener(new ButtonListener());

    // Create JPanel objects and let the variables reference it.
    leftPanel = new JPanel();
    rightPanel = new JPanel();

    // Change the background colors of the panels.
    leftPanel.setBackground(Color.CYAN);
    rightPanel.setBackground(Color.BLUE);

    // Change the Right Message Label's foreground color to white for easy
    // visibility.
    rightMessageLabel.setForeground(Color.WHITE);

    // Add the components to the panels.
    leftPanel.add(leftMessageLabel);
    leftPanel.add(bookSelections);

    rightPanel.add(rightMessageLabel);
    rightPanel.add(shoppingCartItems);
    rightPanel.add(totalTextField);
    rightPanel.add(removeItemButton);
    rightPanel.add(clearItemsButton);
    rightPanel.add(checkoutButton);
}

/**
 * The populateList method reads the book item names and prices from a
 * text file called "BookPrices.txt". Use the bookList ArrayList to 
 * allocate storage for the book names.
 */

private void populateList()
{
    try
    {
        // Open the file.
        File file = new File("BookPrices.txt");
        Scanner inputFile = new Scanner(file);

        // Read each line into the bookList ArrayList.
        for (int index = 0; inputFile.hasNext(); index++)
        {
            bookList.add(inputFile.nextLine());
        }

        // Close the file.
        inputFile.close();
    }
    catch(FileNotFoundException e)
    {
        // Thrown by the Scanner constructor when the file is not found.
        JOptionPane.showMessageDialog(null, "The file BookPrices.txt " +
                                      "does not exist! Check your " +
                                      "directory and make sure the file " +
                                      "is there.");
        System.exit(0);
    }

}

/**
 * ButtonListener is an action listener class for the Remove, Clear, and
 * Checkout buttons.
 */

private class ButtonListener implements ActionListener
{
    /**
     * The actionPerformed method executes when the user clicks on the 
     * Remove, Clear, or Checkout button.
     * @param e The event object.
     */

    public void actionPerformed(ActionEvent e)
    {   
        // Determine which button was clicked and perform 
        // the following action.
        if (e.getSource() == removeItemButton)
        {
            System.out.println("Remove Button clicked.");
        }
        else if (e.getSource() == clearItemsButton)
        {
            System.out.println("Clear Button clicked.");
        }
        else if (e.getSource() == checkoutButton)
        {
            System.out.println("Checkout Button clicked.");
        }
    }

    // Create a list and process all items in that list to find 
    // the prices of the books.
}

public static void main(String[] args)
{
    ShoppingCartSystem mainWindow = new ShoppingCartSystem();
}

}
`

Recommended Answers

All 2 Replies

To see where the computer is looking for the file, print out the File class's absolute path on about line 105.
The print out will show you where the computer is looking for the file.

Thank you! The problem was it was looking in the Project folder and not the source folder where the BookPrices.txt was located :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.