This is the last part to my assignment and when I go to compile the last part I come up with 7 errors unsure what it is that it wants me to do. The errors are:

C:\Users\Owner\Desktop\week9 part6\Inventory6.java:212: error: illegal escape character
                        strPrice = strPrice.replaceAll("\$", "");
                                                         ^
C:\Users\Owner\Desktop\week9 part6\Inventory6.java:270: error: illegal escape character
                        strPrice = strPrice.replaceAll("\$", "");
                                                         ^
C:\Users\Owner\Desktop\week9 part6\Inventory6.java:315: error: illegal escape character
    File theDir = new File("C:\data");
                               ^
C:\Users\Owner\Desktop\week9 part6\Inventory6.java:319: error: illegal escape character
    File theFile = new File("C:\data\inventory.dat");
                                ^
C:\Users\Owner\Desktop\week9 part6\Inventory6.java:319: error: illegal escape character
    File theFile = new File("C:\data\inventory.dat");
                                     ^
C:\Users\Owner\Desktop\week9 part6\Inventory6.java:346: error: illegal escape character
    File theFile = new File("C:\data\inventory.dat");
                                ^
C:\Users\Owner\Desktop\week9 part6\Inventory6.java:346: error: illegal escape character
    File theFile = new File("C:\data\inventory.dat");
                                     ^

This is what I have for my coding for the last part:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.net.URL;
import java.io.*;
import java.util.*;

public class Inventory6 extends JFrame
{

    //create inventory for the office supplies
    Inventory2 ProductInventory2;

    // index in the supply inventory of the currently displayed office supply. starts at 0, goes to the number of supplies in the inventory - 1
    int index = 0;

    // last item number added to the inventory
    int lastNumberAdded = 0;

    // GUI elements to display currently selected office supply's information
    private final JLabel nameLabel = new JLabel(" Product Name: ");
    private JTextField nameText;

    private final JLabel numberLabel = new JLabel(" Product Number: ");
    private JTextField numberText;

    private final JLabel brandLabel = new JLabel(" Brand: ");
    private JTextField brandText;

    private final JLabel priceLabel = new JLabel(" Price: ");
    private JTextField priceText;

    private final JLabel quantityLabel = new JLabel(" Quantity: ");
    private JTextField quantityText;

    private final JLabel valueLabel = new JLabel(" Value: ");
    private JTextField valueText;

    private final JLabel restockFeeLabel = new JLabel(" Restocking Fee: ");
    private JTextField restockFeeText;

    private final JLabel totalValueLabel = new JLabel(" Inventory Total: ");
    private JLabel totalValueText;

    // go to the next office supply in the list
    private Action nextAction  = new AbstractAction("Next") {
        public void actionPerformed(ActionEvent evt) {

        // see if there is a next office supply
        if (index == ProductInventory2.getSize() - 1) {
            // if we're at the last office supply in the list, then we can't go any further
            // so go to the front of the list
            index = 0;
        } else {
            index++;
        }

        repaint();
        }
    };
    private JButton nextButton = new JButton(nextAction);

    // go to the previous office supply in the list
    private Action previousAction  = new AbstractAction("Previous") {
        public void actionPerformed(ActionEvent evt) {

        // if we're at the first office supply, then go to the last office supply in the list
        if (index == 0) {
            index = ProductInventory2.getSize() - 1;
        } else {
            index--;
        }

        repaint();
        }
    };
    private JButton previousButton = new JButton(previousAction);

    // go to the first office supply in the list
    private Action firstAction  = new AbstractAction("First") {
        public void actionPerformed(ActionEvent evt) {

        index = 0;

        repaint();
        }
    };
    private JButton firstButton = new JButton(firstAction);

    // go to the last office supply in the list
    private Action lastAction  = new AbstractAction("Last") {
        public void actionPerformed(ActionEvent evt) {

        index = ProductInventory2.getSize() - 1;

        repaint();
        }
    };
    private JButton lastButton = new JButton(lastAction);

    // change office supply inventory buttons and actions
    Action searchAction = new AbstractAction("Search") {
        public void actionPerformed(ActionEvent evt) {
            // the first time the search button is pressed,
            if (searchButton.getText().equals("Search")) {

                // change the text
                searchButton.setText("GO!");

                // make the name of the current office supply editable
                nameText.setEditable(true);

                // the second time it is pressed,
            } else if (searchButton.getText().equals("GO!")) {

                // make the name of the current office supply non-editable
                nameText.setEditable(false);

                String inputName = nameText.getText();

                // if we went past the end of the list looking,
                int i = ProductInventory2.searchForProductByName(inputName);
                if ( i >= 0 ) {
                    index = i;
                    repaint();

                } else {
                    // display a message indicating the office supply was not found.
                    JOptionPane.showMessageDialog(null, "No Matches Found!");
                }

                searchButton.setText("Search");
            }

            repaint();
        }
    };
    JButton searchButton = new JButton(searchAction);

    // delete button. delete the currently displayed office supply from the inventory. update the display
    Action deleteAction = new AbstractAction("Delete") {
            public void actionPerformed(ActionEvent evt) {

                ProductInventory2 = ProductInventory2.deleteProduct(index);

                // display the previous item in the inventory
                index = index - 1;
                if (index < 0) {
                    index = 0;
                }

                repaint();
            }
        };
    JButton deleteButton = new JButton(deleteAction);

    Action saveAction = new AbstractAction("Save") {
        public void actionPerformed(ActionEvent evt) {
            saveInventory2ToFile();
        }
    };
    JButton saveButton = new JButton(saveAction);

    Action loadAction = new AbstractAction("Load File") {
        public void actionPerformed(ActionEvent evt) {
            loadInventory2FromFile();
        }
    };
    JButton loadButton = new JButton(loadAction);

    Action addAction = new AbstractAction("Add") {
            public void actionPerformed(ActionEvent evt) {
                // if the user clicks add for the first time,
                if (addButton.getText().equals("Add")) {
                    addButton.setText("Click to Add!");

                    // make the fields of the current office supply editable
                    nameText.setEditable(true);
                    numberText.setEditable(true);
                    brandText.setEditable(true);
                    priceText.setEditable(true);
                    quantityText.setEditable(true);

                    nameText.setText("");
                    numberText.setText("" + (lastNumberAdded + 1));
                    brandText.setText("");
                    priceText.setText("$0.00");
                    quantityText.setText("0");

                    valueText.setText("$0.00");
                    restockFeeText.setText("$0.00");

                    // the second time,
                } else if (addButton.getText().equals("Click to Add!")) {

                    try {
                        // Assign text fields to variables
                        String N = nameText.getText().trim();
                        String Num = numberText.getText().trim();
                        String brand = brandText.getText().trim();
                        String stockQty = quantityText.getText().trim();
                        String strPrice = priceText.getText().trim();

                        // Convert the text into int and double.
                        int in_stock = Integer.parseInt ( stockQty );
                        strPrice = strPrice.replaceAll("\$", "");
                        Double price = Double.parseDouble( strPrice );

                        Product newProduct = new Product(brand, Num, N, in_stock, price   );
                        addProductToInventory2(newProduct);

                       // the current office supply is now the last office supply in the list
                       index=ProductInventory2.getSize()- 1;

                    } catch (Exception addException) {

                        JOptionPane.showMessageDialog(null, "Error adding office supply's information:\n" +  addException);
                        index = 0;

                    }

                    addButton.setText("Add");

                    nameText.setEditable(false);
                    numberText.setEditable(false);
                    brandText.setEditable(false);
                    priceText.setEditable(false);
                    quantityText.setEditable(false);

                    repaint();

                }
            }
        };

    JButton addButton = new JButton(addAction);

    Action modifyAction = new AbstractAction("Modify") {
            public void actionPerformed(ActionEvent evt) {
                // if the user clicks add for the first time,
                if (modifyButton.getText().equals("Modify")) {
                    modifyButton.setText("Click to Modify!");

                    // make the fields of the current office supply editable
                    nameText.setEditable(true);
                    numberText.setEditable(true);
                    brandText.setEditable(true);
                    priceText.setEditable(true);
                    quantityText.setEditable(true);

                    // the second time,
                } else if (modifyButton.getText().equals("Click to Modify!")) {

                    try {
                        //Assign the text fields to variables
                        String N = nameText.getText().trim();
                        String Num = numberText.getText().trim();
                        String brand = brandText.getText().trim();
                        String stockQty = quantityText.getText().trim();
                        String strPrice = priceText.getText().trim();

                        //Convert text into int and double.
                        int in_stock = Integer.parseInt ( stockQty );
                        strPrice = strPrice.replaceAll("\$", "");
                        Double price = Double.parseDouble( strPrice );

                        Product newProduct = new Product(brand, Num, N, in_stock, price   );
                        ProductInventory2.setProduct(newProduct, index);

                    } catch (Exception modifyException) {

                        JOptionPane.showMessageDialog(null, "Error modifying office supply's information:\n" +  modifyException);

                    }

                    modifyButton.setText("Modify");

                    nameText.setEditable(false);
                    numberText.setEditable(false);
                    brandText.setEditable(false);
                    priceText.setEditable(false);
                    quantityText.setEditable(false);

                    // update the display
                    repaint();

                }
            }
        };
    JButton modifyButton = new JButton(modifyAction);

    public void addProductToInventory2(Product temp)
    {
        ProductInventory2.addProduct(temp);
        lastNumberAdded = Integer.parseInt(temp.getNumber());
        repaint();
    }
    public void sortProductInventory2()
    {
        ProductInventory2.sortInventory();
        index = 0;
        repaint();
    }

    // write the inventory to the file C:\data\inventory.dat
    public void saveInventory2ToFile() {

    // create the directory if it doesn't already exist
    File theDir = new File("C:\data");
    theDir.mkdir();

    // reference to the file
    File theFile = new File("C:\data\inventory.dat");

    try {
        // create the file, overwriting previous contents
        FileOutputStream fileOut = new FileOutputStream( theFile );

        ObjectOutputStream objOut = new ObjectOutputStream( fileOut );

        objOut.writeObject(ProductInventory2);

        objOut.flush();
        objOut.close();

    } catch (IOException e) {

        JOptionPane.showMessageDialog(null, "Error occured while writing to file:" + e);

    }

    }

    public int loadInventory2FromFile() {

    // save the old inventory just in case
    Inventory2 oldProductInventory2 = ProductInventory2;

    // refernce to the file to read
    File theFile = new File("C:\data\inventory.dat");

    try {

        FileInputStream fileIn = new FileInputStream( theFile );
        ObjectInputStream objIn = new ObjectInputStream( fileIn );

        ProductInventory2 = (Inventory2)objIn.readObject();

        objIn.close();

     } catch (Exception e) {

        // indicates error
        JOptionPane.showMessageDialog(null, "Could not load file:"  + e);

        // restore the old inventory
        ProductInventory2 = oldProductInventory2;
        oldProductInventory2 = null;

        return -1;
    }

    // disregard the old inventory
    oldProductInventory2 = null;

    // update the GUI
    repaint();

    return 0;
    }

public Inventory6(int maximum_number_of_supplies)
    {
        // create the inventory object that will hold the office supply information
        ProductInventory2 = new Inventory2(maximum_number_of_supplies);

        // setup the GUI
        // add the next office supply button to the top of the GUI
        // setup a panel to collect all the buttons in a FlowLayout
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(firstButton);
        buttonPanel.add(previousButton);
        buttonPanel.add(nextButton);
        buttonPanel.add(lastButton);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);

        // setup the other buttons
        JPanel dataButtonPanel = new JPanel();
        dataButtonPanel.add(addButton);
        dataButtonPanel.add(deleteButton);
        dataButtonPanel.add(modifyButton);
        dataButtonPanel.add(searchButton);
        dataButtonPanel.add(saveButton);
        dataButtonPanel.add(loadButton);
        getContentPane().add(dataButtonPanel, BorderLayout.NORTH);

        // setup the logo for the GUI
        URL url = this.getClass().getResource("logo.jpg");
           Image img = Toolkit.getDefaultToolkit().getImage(url);
        // scale the image so it will fit
        Image scaledImage = img.getScaledInstance(205, 300, Image.SCALE_AREA_AVERAGING);
        // create a JLabel with the image as the label's Icon
        Icon logoIcon = new ImageIcon(scaledImage);
        JLabel companyLogoLabel = new JLabel(logoIcon);

        // add logo to GUI
        getContentPane().add(companyLogoLabel, BorderLayout.WEST);

        // product information
        // setup a panel to collect all the components.
        JPanel centerPanel = new JPanel(new GridLayout(8, 2, 0, 4));

        centerPanel.add(nameLabel);
        nameText = new JTextField("");
        nameText.setEditable(false);
        centerPanel.add(nameText);

        centerPanel.add(numberLabel);
        numberText = new JTextField("");
        numberText.setEditable(false);
        centerPanel.add(numberText);

        centerPanel.add(brandLabel);
        brandText = new JTextField("");
        brandText.setEditable(false);
        centerPanel.add(brandText);

        centerPanel.add(priceLabel);
        priceText = new JTextField("");
        priceText.setEditable(false);
        centerPanel.add(priceText);

        centerPanel.add(quantityLabel);
        quantityText = new JTextField("");
        quantityText.setEditable(false);
        centerPanel.add(quantityText);

        centerPanel.add(valueLabel);
        valueText = new JTextField("");
        valueText.setEditable(false);
        centerPanel.add(valueText);

        centerPanel.add(restockFeeLabel);
        restockFeeText = new JTextField("");
        restockFeeText.setEditable(false);
        centerPanel.add(restockFeeText);

        // add the overall inventory information to the panel
        centerPanel.add(totalValueLabel);
        totalValueText = new JLabel("");
        centerPanel.add(totalValueText);

        // add the panel to the center of the GUI's window
        getContentPane().add(centerPanel, BorderLayout.CENTER);

        repaint();
    }

    // repaint the GUI with new office supply information
    public void repaint() {

        Product temp = ProductInventory2.getProduct(index);

        if (temp != null) {
            nameText.setText( temp.getName() );
            numberText.setText( ""+temp.getNumber() );
            brandText.setText( temp.getBrandName() );
            priceText.setText( String.format("$%.2f", temp.getPrice()) );
            quantityText.setText( ""+temp.getQuantity() );
            valueText.setText( String.format("$%.2f", temp.getPrice() * temp.getQuantity() ) );
            restockFeeText.setText( String.format("$%.2f", temp.getRestockFee() ) );
        }

        totalValueText.setText( String.format("$%.2f", ProductInventory2.getTotalValueOfAllInventory() ) );

    }

    public static void main(String args[])
    {

        // create a new GUI object that will hold a maximum of 40 office supplies
        Inventory6 Product_GUI = new Inventory6 (40);

        // Add the office supplies to the inventory
        Product_GUI.addProductToInventory2(new Product("Gel Glide" , "1","Rollerball Pens" , 26, 1.00));
        Product_GUI.addProductToInventory2(new Product("Sharpie" , "2","Markers" , 23,  2.00));
        Product_GUI.addProductToInventory2(new Product("Bic",  "3","White-out", 7,  3.00));
        Product_GUI.addProductToInventory2(new Product("Generic", "4","Lead Pencils" , 12, 4.00));
                Product_GUI.addProductToInventory2(new Product("Crayola", "5", "Crayons", 12, 5.00));
                Product_GUI.addProductToInventory2(new Product("Rose Art", "6", "Paint Set", 12, 6.00));                

        // sort the office supplies by name
        Product_GUI.sortProductInventory2();

        // restore the save inventory if it exists
        Product_GUI.loadInventory2FromFile();

        // Get the GUI to show up on screen
        Product_GUI.setDefaultCloseOperation( EXIT_ON_CLOSE );
        Product_GUI.pack();
        Product_GUI.setSize(700, 350);
        Product_GUI.setResizable(false);
        Product_GUI.setLocationRelativeTo( null );
        Product_GUI.setVisible(true);

        return;

    }
}  // End Inventory6 class

Recommended Answers

All 5 Replies

try replacing '\' in those lines with '\'

\\ he means :)

commented: yes indeed ... typo :) +13

Thanks it worked but now I am still having 2 errors with the same code don't know why, which are:

C:\Users\Owner\Desktop\week9 part6\Inventory6.java:212: error: illegal escape character
                        strPrice = strPrice.replaceAll("\$", "");
                                                         ^
C:\Users\Owner\Desktop\week9 part6\Inventory6.java:270: error: illegal escape character
                        strPrice = strPrice.replaceAll("\$", "");
                                                         ^

Exactly the same reason, exactly the same fix. If you want a \ in your Regex then you have to code two \ chars in the Java String literal, because \ is used in Java String Literals for special characters like \n (newline)

Thanks, I got it I took out the \ and put $% instead and it worked. Thanks for your input.

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.