Search code examples
javaswingjtablejtextfieldfilereader

How to edit a txt file using JTextField with a specific format?


I am creating a inventory program that involves the cashier and the admin mode. The admin mode can change the item name, price and stocks of an item. All items are stored in a txt file.

The content of the txt file (it has slashes to separate names, prices etc):

 ID     Item Name                 Price   Stocks
00001 / Xbox 360                 / 7999  / 10
00002 / Xbox Series X            / 25999 / 20
00003 / Playstation 4 Pro        / 22999 / 15
00004 / Nintendo Switch          / 17999 / 27
00005 / Nintendo Switch Lite     / 11999 / 15

I read and display the items using a JTable. The column headers are added in the code:

String filePath = "C:\\Users\\zagad\\IdeaProjects\\DATABYTES\\stock\\consoles\\consoles.txt";
                File file = new File(filePath);
                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    DefaultTableModel model = (DefaultTableModel) productTable.getModel();
                    Object[] tableLines = br.lines().toArray();

                    for (int i = 0; i < tableLines.length; i++) {
                        String line = tableLines[i].toString().trim();
                        String[] dataRow = line.split("/");
                        productTable.getColumnModel().getColumn(0).setPreferredWidth(21);
                        productTable.getColumnModel().getColumn(1).setPreferredWidth(120);
                        productTable.getColumnModel().getColumn(2).setPreferredWidth(30);
                        productTable.getColumnModel().getColumn(3).setPreferredWidth(30);
                        model.addRow(dataRow);
                    }

Here is a reference image of the layout:

enter image description here

It has a JTable and four JTextFields. Is there any idea that I can edit the txt file directly what the user inputs from the JTextFields with the corresponding location of items in the txt file?

Any idea is appreciated. Thank you!


Solution

  • Because your file data lines (data records) are formatted in a specific way with various spacing, you should be trimming off any leading or trailing white-spaces from the columnar data as it is read to be placed within the JTable. Currently, you split each data file row with:

    String[] dataRow = line.split("/"); 
    

    This will not remove all the white-space padding applied to each column of data that was placed upon that data when it was formatted and saved to the file. This padding will need to be dealt with (removed) when you actually need to work with that data. It is far better to remove al this white-space padding when you carry out the split, like this:

    String[] dataRow = line.split("\\s{0,}/\\s{0,}");
    

    You have disclosed very little information with regards to what your particular application really contains like data types (although most are obvious), what is already created (classes, member/instance variables, fields, statics, etc), the code used to save your data from JTable to file, etc, etc. This therefore leaves the whole works open to assumptions or 101 questions which I in particular will not ask. Because of this, the code I present below will be basic and more geared to a Console styled inventory system rather than a GUI styled system. You will need to apply the code to specific areas of your application where you see fit.

    To start with, you need an Items class. Since each Inventory Item contains specific attributes such as ID, Item Name, Price, and Quantity (in Stock), you need a means to hold this data in memory so that it can be worked with in a fashion where any piece of data can be quickly acquired and manipulated. Your class might look like this:

    public class InventoryItems {
    
        private String id;
        private String itemName;
        private double price;
        private int quantity;
    
        public InventoryItems() {    }
    
        public InventoryItems(String id, String itemName, double price, int quantity) {
            this.id = id;
            this.itemName = itemName;
            this.price = price;
            this.quantity = quantity;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getItemName() {
            return itemName;
        }
    
        public void setItemName(String itemName) {
            this.itemName = itemName;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    
        public int getQuantity() {
            return quantity;
        }
    
        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }
    
        @Override
        public String toString() {
            return new StringBuffer("").append("ID: ").append(id).append(", Item Name: ")
                                       .append(itemName).append(", Price: $").append(price)
                                       .append(", Quantity: ").append(quantity).toString();
        }
    
        /**
         * Returns instance data as a String in the format specified in your supplied 
         * format string.
         * 
         * @param formatString
         * @return
        */
        public String toString(String formatString) {
            if (formatString == null || formatString.equals("")) {
                return toString();
            }
            // String Format Specifiers must be separated 
            // with a single white-space.
            switch (formatString.split("\\s+").length) {
                case 1:
                    return String.format(formatString, id);
                case 2:
                    return String.format(formatString, id, itemName);
                case 3:
                    return String.format(formatString, id, itemName, price);
                case 4:
                    return String.format(formatString, id, itemName, price, quantity);
                default:
                    return toString();
            }
        }
    }
    

    Now you need helper methods to: load your file data (inventory records), retrieve a specific record, retrieve specific item attributes, save specific item attributes, save records, etc, etc. These methods might look like this:

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Scanner;
    
    
    public class InventoryDemo {
    
    public enum ErrorMsg {
        LOAD_ERR("Inventory Items have not been loaded in yet!"),
        NOT_ADMIN("Invalid Operation! Admin Status Required!"),
        ID_EXISTS("Invalid ID! That ID Already Exists!"),
        NAME_EXISTS("Invalid Item Name! That Name Already Exists!");
    
        private String msg;
    
        ErrorMsg(String msg) {
            this.msg = msg;
        }
    
        public String msg() {
            return msg;
        }
    
    }
    
    private String encoding = "UTF-8"; // options: "US-ASCII", "UTF-8", "UTF-16"
    private final List<InventoryItems> items = new ArrayList<>();
    private boolean isInAdminMode = true;
    private String dataFile = "InventoryItems.txt";
    private String ls = System.lineSeparator();
    
    public static void main(String[] args) {
        // App is started this way to avoid the need for statics.
        new InventoryDemo().startApp(args);
    }
    
    private void startApp(String[] args) {
        if (!loadDataFile()) {
            System.err.println("Error Loading: " + dataFile);
            System.exit(0);
        }
    
        Scanner input = new Scanner(System.in);
    
        System.out.println("This code is run from the startApp() method" + ls 
                         + "located within the InventoryDemo class. Its" + ls
                         + "intent is to demonstrate the how the methods" + ls
                         + "withint this class function." );
        pressAnyKey(input);
    
        System.out.println("Available Inventory Items");
        System.out.println("=========================");
        String[] allItems = getAllInventoryItems();
        if (allItems != null) {
            for (String strg : allItems) {
                System.out.println(strg);
            }
        }
    
        pressAnyKey(input);
    
        showFormattedTable();
    
        pressAnyKey(input);
    
        System.out.println();
        System.out.println("Available Inventory Items (with formated ID & Name)");
        String header = String.format("%-7s %-26s", "ID", "Item Name");
        String topLine = String.join("", Collections.nCopies(header.length(), "="));
        String underline = String.join("", Collections.nCopies(header.length(), "-"));
        System.out.println(topLine);
        System.out.println(header);
        System.out.println(underline);
        allItems = getAllInventoryItems("%-7s %-26s");
        if (allItems != null) {
            for (String strg : allItems) {
                System.out.println(strg);
            }
        }
        System.out.println(topLine);
    
        pressAnyKey(input);
    
        System.out.println();
        System.out.println("Retrieve A Specific Record (by ID: 00004)");
        System.out.println("=========================================");
        String itemString = getItemString("00004");
        System.out.println(itemString);
    
        pressAnyKey(input);
    
        System.out.println();
        System.out.println("Retrieve A Specific ID (by Name: Playstation 4 Pro)");
        System.out.println("===================================================");
        itemString = getItemID("Playstation 4 Pro");
        System.out.println(itemString);
    
        pressAnyKey(input);
    
        System.out.println();
        System.out.println("Retrieve A Specific Name (by ID: 00004)");
        System.out.println("=======================================");
        itemString = getItemName("00004");
        System.out.println(itemString);
    
        pressAnyKey(input);
    
        System.out.println();
        System.out.println("Retrieve A Specific Price (by ID: 00003)");
        System.out.println("========================================");
        double itemprice = getItemPrice("00003");
        System.out.println(itemprice);
    
        pressAnyKey(input);
    
        System.out.println();
        System.out.println("Retrieve A Specific Quantity (by ID: 00002)");
        System.out.println("===========================================");
        int itemQuant = getItemQuantity("00002");
        System.out.println(itemQuant);
    
        pressAnyKey(input);
    
        System.out.println();
        System.out.println("Change Name for: 'Playstation 4 Pro' at ID: "
                + "'00003' TO 'Some Kinda Game-Box'");
        System.out.println("============================================"
                + "================================");
        itemString = getItemString("00003");
        System.out.println("Current Name: -->  " + itemString);
        setItemName("00003", "Some Kinda Game-Box");
        itemString = getItemString("00003");
        System.out.println("Name is Now:  -->  " + itemString);
    
        pressAnyKey(input);
    
        System.out.println();
        String itemToAdd = "00006/LG LED 60\" Television/70555.0/08";
        System.out.println("Add a new Inventory Item (duplicate ID's are not allowed).");
        System.out.println("Adding (with 'NO Auto-ID-Increment'): "
                + itemToAdd);
        System.out.println("======================================"
                + "======================================");
        if (addInventoryItem(itemToAdd)) {
            showFormattedTable();
        }
        else {
            System.err.println("Could not add (" + itemToAdd + ") to Records List!");
        }
    
        pressAnyKey(input);
    
        System.out.println();
        itemToAdd = "DYSON 430 Vacume/65437.0/6";
        System.out.println("Add a new Inventory Item (duplicate ID's are not allowed).");
        System.out.println("Adding (with 'Auto-ID-Increment'): "
                + itemToAdd);
        System.out.println("======================================"
                + "=======================");
        if (addInventoryItem(itemToAdd)) {
            showFormattedTable();
        }
        else {
            System.err.println("Could not add (" + itemToAdd + ") to Records List!");
        }
    
        pressAnyKey(input);
    
        System.out.println();
        System.out.println("Delete an Inventory Item ID (based on Item ID: 00006).");
        String itemIDToDelete = "00006";
        if (deleteInventoryItemID(itemIDToDelete)) {
            showFormattedTable();
        }
        else {
            System.err.println("Could not delete record ID (" + itemIDToDelete + ") from Records List!");
        }
    
        pressAnyKey(input);
    
        System.out.println();
        System.out.println("Delete an Inventory Item Name (based on Item Name: DYSON 430 Vacume).");
        String itemNameToDelete = "DYSON 430 Vacume";
        if (deleteInventoryItemName(itemNameToDelete)) {
            showFormattedTable();
        }
        else {
            System.err.println("Could not delete record Item Name (" + itemNameToDelete + ") from Records List!");
        }
    }
    
    private void showFormattedTable() {
        System.out.println();
        System.out.println("   Available Inventory Items (with formatting)");
        String header = String.format("%-7s %-26s %-8s %-5s", "ID", "Item Name", "Price", "Stock");
        String topLine = String.join("", Collections.nCopies(header.length(), "="));
        String underline = String.join("", Collections.nCopies(header.length(), "-"));
        System.out.println(topLine);
        System.out.println(header);
        System.out.println(underline);
        String[] allItems = getAllInventoryItems("%-7s %-26s $%-9s %-5s");
        if (allItems != null) {
            for (String strg : allItems) {
                System.out.println(strg);
            }
        }
        System.out.println(topLine);
    }
    
    /**
     * Deletes the Inventory Item in Records List and data file that contains the 
     * supplied Item ID.<br><br>
     * 
     * @param idToDelete (String) The ID Number of the record to delete.<br>
     * 
     * @return (Boolean) True if successful and False if not.
     */
    public boolean deleteInventoryItemID(String idToDelete) {
        if (!isInAdminMode) {
            System.err.println(ErrorMsg.NOT_ADMIN.msg);
            return false;
        }
        if (items.isEmpty()) {
            System.err.println(ErrorMsg.LOAD_ERR.msg);
            return false;
        }
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).getId().equals(idToDelete)) {
                items.remove(i);
                break;
            }
        }
        refreshDataBase();
        return true;
    }
    
    /**
     * Deletes ALL Inventory Items in Records List and data file that contains the 
     * supplied Item Name.<br><br>
     * 
     * @param nameToDelete (String) The Item Name to delete (case insensitive).<br>
     * 
     * @return (Boolean) True if successful and False if not.
     */
    public boolean deleteInventoryItemName(String nameToDelete) {
        if (!isInAdminMode) {
            System.err.println(ErrorMsg.NOT_ADMIN.msg);
            return false;
        }
        if (items.isEmpty()) {
            System.err.println(ErrorMsg.LOAD_ERR.msg);
            return false;
        }
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).getItemName().equalsIgnoreCase(nameToDelete)) {
                items.remove(i);
                i--;
            }
        }
        refreshDataBase();
        return true;
    }
    
    /**
     * Admin Rights required!<br><BR>
     * <p>
     * Adds a Inventory Item to the Records List and database file.<br><br>
     * This method can produce a unique ID string automatically if (and only if)
     * an ID value is <b>not</b> supplied within the Inventory Item string. If
     * an ID <b>is supplied</b> within the Inventory Item string then that ID
     * will be used <b>UNLESS</b> that particular ID already exists in which
     * case this method will not apply the addition and a message is displayed
     * to Console indicating as such.
     *
     * @param itemToAdd (String) The string to be supplied must be a forward
     *                  slash (<b>/</b>) delimited <b>String</b> consisting
     *                  consisting of the 4 data parts that make up any
     *                  particular Inventory Item:<pre>
     *
     *     1) The unique Item ID String (optional);
     *     2) The Item Name String;
     *     3) The Item Price (as a String);
     *     4) The Item Quantity (as a String).</pre><br>
     *
     * A typical Inventory Item String supplied to this method should look like:
     * <pre>
     *     "00006/LG LED 60\" Television/70555.0/08"</pre><br>
     *
     * Or it can look like (if an Auto-ID is requested):
     * <pre>
     *     "LG LED 60\" Television/70555.0/08"</pre><br>
     *
     * Providing duplicate ID's (an ID that is already in the database file or
     * records List) is not allowed.
     *
     * @return (Boolean) True if the addition is successful and False if not.
     */
    public boolean addInventoryItem(String itemToAdd) {
        if (!isInAdminMode) {
            System.err.println(ErrorMsg.NOT_ADMIN.msg);
            return false;
        }
        try {
            String[] itemParts = itemToAdd.split("\\s{0,}/\\s{0,}");
    
            // Is Auto-ID-Increment required?
            if (!itemParts[0].matches("\\d+") && itemParts.length == 3) {
                //Yes...
                String newID = getUniqueID();
                String[] parts = new String[4];
                parts[0] = newID;
                parts[1] = itemParts[0];
                parts[2] = itemParts[1];
                parts[3] = itemParts[2];
                itemParts = parts;
            }
            String id = itemParts[0];
            if (doesItemIDAlreadyExist(id)) {
                System.err.println("The Item ID (" + id + ") is an " + ErrorMsg.ID_EXISTS.msg);
                return false;
            }
            String name = itemParts[1];
            if (doesItemNameAlreadyExist(name)) {
                System.err.println("The Item Name (" + name + ") is an " + ErrorMsg.NAME_EXISTS.msg);
                return false;
            }
            double price = Double.parseDouble(itemParts[2]);
            int quant = Integer.parseInt(itemParts[3]);
            items.add(new InventoryItems(id, name, price, quant));
            refreshDataBase();
        }
        catch (Exception ex) {
            System.err.println(ex);
            return false;
        }
        return true;
    }
    
    public boolean doesItemIDAlreadyExist(String id) {
        boolean res = false;
        if (items.isEmpty()) {
            res = false;
        }
        else {
            for (InventoryItems itms : items) {
                if (itms.getId().equals(id)) {
                    res = true;
                    break;
                }
            }
        }
        return res;
    }
    
    public boolean doesItemNameAlreadyExist(String name) {
        boolean res = false;
        if (items.isEmpty()) {
            res = false;
        }
        else {
            for (InventoryItems itms : items) {
                if (itms.getItemName().equalsIgnoreCase(name)) {
                    res = true;
                    break;
                }
            }
        }
        return res;
    }
    
    public String getUniqueID() {
        if (items.isEmpty()) {
            System.err.println(ErrorMsg.LOAD_ERR.msg);
            return null;
        }
        int max = 0;
        for (InventoryItems itms : items) {
            String idVal = itms.getId();
            if (!idVal.matches("\\d+")) {
                continue;
            }
            int val = Integer.parseInt(idVal);
            if (val > max) {
                max = val;
            }
        }
        if (max == 0) {
            return String.format("%5s", 1).replace(' ', '0');
        }
        else {
            return String.format("%5s", (max + 1)).replace(' ', '0');
        }
    }
    
    public String[] getAllInventoryItems(String... formatString) {
        if (items.isEmpty()) {
            System.err.println(ErrorMsg.LOAD_ERR.msg);
            return null;
        }
        String format = "";
        if (formatString.length > 0) {
            format = formatString[0];
        }
        String[] itms = new String[items.size()];
        for (int i = 0; i < items.size(); i++) {
            if (!format.equals("")) {
                itms[i] = items.get(i).toString(format);
            }
            else {
                itms[i] = items.get(i).toString();
            }
        }
        return itms;
    }
    
    public String getItemID(String itemName) {
        if (items.isEmpty()) {
            System.err.println(ErrorMsg.LOAD_ERR.msg);
            return null;
        }
        String res = null;
        for (InventoryItems item : items) {
            if (item.getItemName().equalsIgnoreCase(itemName)) {
                res = item.getId();
                break;
            }
        }
        return res;
    }
    
    public void setItemID(String itemName, String newIDValue) {
        if (!isInAdminMode) {
            System.err.println(ErrorMsg.NOT_ADMIN.msg);
            return;
        }
    
        if (items.isEmpty()) {
            System.err.println(ErrorMsg.LOAD_ERR.msg);
            return;
        }
        for (InventoryItems item : items) {
            if (item.getItemName().equalsIgnoreCase(itemName)) {
                item.setId(newIDValue);
                break;
            }
        }
        refreshDataBase();
    }
    
    public String getItemString(String idNumber) {
        if (items.isEmpty()) {
            System.err.println(ErrorMsg.LOAD_ERR.msg);
            return null;
        }
        String res = null;
        for (InventoryItems item : items) {
            if (item.getId().equals(idNumber)) {
                res = item.toString();
                break;
            }
        }
        return res;
    }
    
    public String getItemName(String idNumber) {
        if (items.isEmpty()) {
            System.err.println(ErrorMsg.LOAD_ERR.msg);
            return null;
        }
        String res = null;
        for (InventoryItems item : items) {
            if (item.getId().equals(idNumber)) {
                res = item.getItemName();
                break;
            }
        }
        return res;
    }
    
    public void setItemName(String idNumber, String newName) {
        if (!isInAdminMode) {
            System.err.println(ErrorMsg.NOT_ADMIN.msg);
            return;
        }
        if (items.isEmpty()) {
            System.err.println(ErrorMsg.LOAD_ERR.msg);
            return;
        }
        for (InventoryItems item : items) {
            if (item.getId().equals(idNumber)) {
                item.setItemName(newName);
                break;
            }
        }
        refreshDataBase();
    }
    
    private double getItemPrice(String idNumber) {
        if (items.isEmpty()) {
            System.err.println(ErrorMsg.LOAD_ERR.msg);
            return 0.0d;
        }
        double res = 0.0d;
        for (InventoryItems item : items) {
            if (item.getId().equals(idNumber)) {
                res = item.getPrice();
                break;
            }
        }
        return res;
    }
    
    public void setItemPrice(String idNumber, double newPrice) {
        if (!isInAdminMode) {
            System.err.println(ErrorMsg.NOT_ADMIN.msg);
            return;
        }
        if (items.isEmpty()) {
            System.err.println(ErrorMsg.LOAD_ERR.msg);
            return;
        }
        for (InventoryItems item : items) {
            if (item.getId().equals(idNumber)) {
                item.setPrice(newPrice);
                break;
            }
        }
        refreshDataBase();
    }
    
    public int getItemQuantity(String idNumber) {
        if (items.isEmpty()) {
            System.err.println(ErrorMsg.LOAD_ERR.msg);
            return 0;
        }
        int res = 0;
        for (InventoryItems item : items) {
            if (item.getId().equals(idNumber)) {
                res = item.getQuantity();
                break;
            }
        }
        return res;
    }
    
    public void setItemQuantity(String idNumber, int newQuantity) {
        if (!isInAdminMode) {
            System.err.println(ErrorMsg.NOT_ADMIN.msg);
            return;
        }
        if (items.isEmpty()) {
            System.err.println(ErrorMsg.LOAD_ERR.msg);
            return;
        }
        for (InventoryItems item : items) {
            if (item.getId().equals(idNumber)) {
                item.setQuantity(newQuantity);
            }
        }
        refreshDataBase();
    }
    
    public boolean loadDataFile() {
        boolean success;
        items.clear();  // Clear the Records Data List.
    
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(dataFile), encoding))) {
        // try (BufferedReader reader = new BufferedReader(new FileReader(dataFile))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // Trim the data line and remove BOM's (if any).
                line = trimBOMs(line);
                // Skip the header line and blank lines (if any).
                if (line.equals("") || line.toLowerCase().startsWith("id")) {
                    continue;
                }
                String[] lineParts = line.split("\\s{0,}/\\s{0,}");
                // Validate record data types...
                String id = "";
                String name = "Unknown";
                double price = 0.0d;
                int quant = 0;
                if (lineParts.length >= 1) {
                    // Is it an actual integer value string
                    if (lineParts[0].matches("\\d+")) {
                        id = lineParts[0];
                    }
                    if (lineParts.length >= 2) {
                        // Does the string contain a name or is it empty.
                        name = (lineParts[1].equals("") ? "Unknown" : lineParts[1]);
                        if (lineParts.length >= 3) {
                            // Is it an actual integer or double/float value string
                            if (lineParts[2].matches("-?\\d+(\\.\\d+)?")) {
                                price = Double.parseDouble(lineParts[2]);
                            }
                            if (lineParts.length >= 4) {
                                // Is it an actual integer value string
                                if (lineParts[0].matches("\\d+")) {
                                    quant = Integer.parseInt(lineParts[3]);
                                }
                            }
                        }
                    }
                }
                items.add(new InventoryItems(id, name, price, quant));
            }
            success = true;
        }
        catch (FileNotFoundException ex) {
            System.err.println(ex);
            success = false;
        }
        catch (IOException ex) {
            System.err.println(ex);
            success = false;
        }
        return success;
    }
    
    public boolean saveItemsToFile() {
        if (!isInAdminMode) {
            System.err.println(ErrorMsg.NOT_ADMIN.msg);
            return false;
        }
        String header = String.format("%-8s%-27s%-10s%-5s%n", "ID", "Item Name", "Price", "Stocks");
    
        try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(dataFile), encoding))) {
        // try (PrintWriter writer = new PrintWriter(new File(dataFile))) {
            writer.append(header);
            for (int i = 0; i < items.size(); i++) {
                String id = items.get(i).getId();
                String name = items.get(i).getItemName();
                double price = items.get(i).getPrice();
                int quant = items.get(i).getQuantity();
                String dataLine = String.format("%-6s/ %-25s/ %-8s/ %-4s%n", id, name, price, quant);
                writer.append(dataLine);
            }
            writer.flush();
        }
        catch (FileNotFoundException | UnsupportedEncodingException ex) {
            System.err.println(ex);
            return false;
        }
        return true;
    }
    
    public void refreshDataBase() {
        saveItemsToFile();
        loadDataFile();
    
        // Call and method to update your JTable(s) here!
    }
    
    public boolean isIsInAdminMode() {
        return isInAdminMode;
    }
    
    public void setIsInAdminMode(boolean isInAdminMode) {
        this.isInAdminMode = isInAdminMode;
    }
    
    public String getDataFilePath() {
        return dataFile;
    }
    
    public void setDataFilePath(String dataFile) {
        this.dataFile = dataFile;
    }
    
    public String getEncoding() {
        return encoding;
    }
    
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }
    
    /**
     * Removes (trims away) leading and trailing white-spaces and Removes 
     * any BOM's (Byte Order Marks) from all the different Character Encodings.<br><br>
     * 
     * @param inputString (String) The Unicode string to remove BOM's from.<br>
     * 
     * @return (String) 
     */
    private static String trimBOMs(String inputString) {
        inputString = inputString.trim()
                .replaceAll("\uFEFF|\uEFBBBF|\uFFFE|\u0000FEFF|\uFFFE0000|\u2B2F7638","")
                .replaceAll("\u2B2F7639|\u2B2F762B|\u2B2F762F|\u2B2F76382D|\uF7644C", "")
                .replaceAll("\uDD736673|\u0EFEFF|\uFBEE28|\u84319533", "");
        return inputString;
    }
    
    private void pressAnyKey(Scanner input) {
        System.out.println(ls + "Press Enter key to continue (q to quit)...");
        String anyKey = input.nextLine();
        if (anyKey.toLowerCase().equals("q")) {
            System.exit(0);
        }
    }
    }
    

    Create a new Java Application project named InventoryDemo and copy/paste the supplied classes into it. This should give you an idea of how to create the tools to do what you need.