Results 1 to 10 of 10
  1. #1
    daud is offline Member
    Join Date
    Aug 2009
    Posts
    7
    Rep Power
    0

    Exclamation cannot find symbol constructor

    hi
    iam a noob and i have a compile error on two lines of my program, the error is the same for both lines

    symbol : constructor Item(int,int,java.lang.String,java.lang.String,jav a.lang.String)

    and the two lines are
    1). }
    public Item() {
    this(0, 0, "", "", "");
    }
    and
    2). Item newIte = new Item
    (Integer.parseInt(txtValue.getText()),
    txtItemName.getText(), txtNotes.getText(), txtFileName.getText (), choiceList.getAction());

    i appreciate any help i can get with this thank you.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'd suggest that you post your current class code but use code tags when posting this.

    To do this, place the tag [code] before your block of code and [/code] after your block of code.

  3. #3
    daud is offline Member
    Join Date
    Aug 2009
    Posts
    7
    Rep Power
    0

    Default

    thank you for the reply sorry about the noobiness here is my complete code

    Java Code:
    package amended;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.*;
    import java.awt.event.*;
    
    // This program allows a list of itemss to be maintained i.e.
    // added, deleted and sorted.  The items are displayed in
    // a JTable and also maintained in an ArrayList.
    
    
    
    class Item implements Comparable {
        private int value;
        private String itemname;
        private String notes;
        private String filename;
        private String choice;
        
        public Item(int v, String i, String n, String f, String c) {
            value = v;
            itemname = i;
            notes = n;
            filename = f;
            choice = c;
        }
        public Item() {
           this(0, 0, "", "", "");
        }
        public int getValue() {
            return value;
        }
        public String getItemName() {
            return itemname;
        }
         public String getNotes() {
            return notes;
        }
          public String getFileName() {
            return filename;
        }
           public String getChoice() {
            return choice;
        }
    // compareTo() allows Item instances in a container
    // (e.g. an ArrayList) to be sorted by Collections.sort().
        
    // It can only sort things in one order (e.g. ascending order of
    // name).  Need to investigate the use of Comparator objects to
    // allow different sort orders to be defined.
        
        public int compareTo(Object o) {
            Item temp = (Item) o;
            
    // Strings already implement compareTo() so just use that
            return (itemname.compareTo(temp.itemname));
        }
    }
    public class MyItems extends JFrame {
        
    // A container for the Item instances
        ArrayList items = new ArrayList();
        
    // GUI bits and bobs
        JPanel panInput = new JPanel(new GridLayout(2,1)),
                panAdd = new JPanel(),
                panDelSort = new JPanel();
        String[] choiceStrings = { "New", "used"};
    
        JTextField txtItemName = new JTextField(030);            
            JTextField txtValue = new JTextField(04);
                JTextField txtNotes = new JTextField(040);
                JTextField txtFileName = new JTextField(010);
                JComboBox choiceList = new JComboBox(choiceStrings);
    
        JButton btnAdd = new JButton("Add"),            
                btnDelete = new JButton("Delete"),
                btnSort = new JButton("Sort Products");
        
        JTable tab = new JTable();
        
    // The DefaultTableModel will allow the contents of the
    // JTable to be manipulated.
        DefaultTableModel tabMod = new DefaultTableModel();
        
        
        
        public MyItems() {
            super("Item list");
            tab.setModel(tabMod);
            tabMod.addColumn("Item Name");
            tabMod.addColumn("Notes on item");
            tabMod.addColumn("Item value");
            tabMod.addColumn("new/second hand item");
            tabMod.addColumn("Image file");
            
            btnAdd.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    addItem();
                }
            });
            
            btnDelete.setToolTipText("Select the records to delete first");
            btnDelete.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    deleteItem();
                }
            });
            btnSort.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    sortItems();
                }
            });
            
            
            panAdd.add(new JLabel("Item Name:"));
            panAdd.add(txtItemName);
            
            panAdd.add(new JLabel("Item Notes:"));
            panAdd.add(txtNotes);
            
            panAdd.add(new JLabel("Price"));
            panAdd.add(txtValue);
            
            panAdd.add(new JLabel("File Name"));
            panAdd.add(txtFileName);
            
            panAdd.add(new JLabel("New/second hand"));
            panAdd.add(choiceList);
            
            panAdd.add(btnAdd);
            panDelSort.add(btnDelete);
            
            panDelSort.add(btnSort);
            panInput.add(panAdd);
            panInput.add(panDelSort);
            getContentPane().add(panInput, BorderLayout.PAGE_END);
            getContentPane().add(new JScrollPane(tab));
            
        }
    // Delete a product from the ArrayList and from the JTable
        public void deleteItem() {
            int [] rows = tab.getSelectedRows();
            for (int i = 0; i < rows.length; i++) {
                tabMod.removeRow(rows[i] - i); // delete from JTable
                items.remove(rows[i] - i); // delete from ArrayList
            }
        }
        
    // Sort the items in the ArrayList and then
    // reload them into the JTable in the sorted order
        
        public void sortItems() {
            Collections.sort(items); // sorts into name order
            loadItemsIntoTable();
        }
        
    // Add a new items to the ArrayList and the JTable
        public void addItem() {
            String [] row = new String [5];
            Item newIte = new Item
                    (Integer.parseInt(txtValue.getText()), 
                    txtItemName.getText(), txtNotes.getText(), txtFileName.getText(), choiceList.getAction());
            
            items.add(newIte); // add to the ArrayList
            row[0] = newIte.getItemName();
            row[1] = newIte.getNotes();
            row[2] = Integer.toString(newIte.getValue());
            row[3] = newIte.getFileName();
            row[4] = newIte.getChoice();
            tabMod.addRow(row); // add to the JTable
        }
        
           
       
        public void loadItemsIntoTable() {
            tabMod.setRowCount(0);
            
            String [] row = new String [5];
            for (Iterator i = items.iterator(); i.hasNext();) {
                Item temp = (Item) i.next();
                row[0] = temp.getItemName();
                row[1] = temp.getNotes();
                row[2] = Integer.toString(temp.getValue());
                row[3] = temp.getItemName();
                row[4] = temp.getChoice();
                tabMod.addRow(row);
            }
        }
        public static void main(String [] a){
            MyItems me = new MyItems();
            me.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0) ;
                }
            });
            me.pack();
            me.setVisible(true);
        }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks for posting the code as this makes it clear. To re-demonstrate
    Java Code:
      public Item(int v, String i, String n, String f, String c) {
        value = v;
        itemname = i;
        notes = n;
        filename = f;
        choice = c;
      }
      public Item() {
        this(0, 0, "", "", "");
      }
    The first Item constructor takes an int and then 4 Strings as its parameters. Your second Item constructor, the default constructor, attempts to call this first constructor, but its parameters are wrong as it is passing 2 ints and then 3 strings. Change it to 1 int and 4 strings and all will be happy.

  5. #5
    daud is offline Member
    Join Date
    Aug 2009
    Posts
    7
    Rep Power
    0

    Default

    thanks i new it was something so simple i changed

    Java Code:
     public Item() {
           this(0, "", "", "", "");
        }
    but i still have the same error on this line

    Java Code:
    Item newIte = new Item
                    (Integer.parseInt(txtValue.getText()), 
                    txtItemName.getText(), txtNotes.getText(), txtFileName.getText(), choiceList.getAction());

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    No, you don't have exactly the same error there, it's the same type of error but is slightly different. The compiler error message will tell you exactly what is wrong. So using this and your knowledge of the constructor and how it needs to be called you should now be able to figure it out on your own. Best of luck!

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    A hint... the last parameter is wrong. You need to get the selected value and cast it to a string. If you don't know how to do that, read the API docs!
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  8. #8
    daud is offline Member
    Join Date
    Aug 2009
    Posts
    7
    Rep Power
    0

    Default

    thank you for all your help i know i have to add actionlistner for my combobox thats what iam doing now

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    That is not how you add actionlisteners to anything. I strongly suggest that you go through the Sun Swing tutorials to learn how to do this.

  10. #10
    daud is offline Member
    Join Date
    Aug 2009
    Posts
    7
    Rep Power
    0

    Default

    thanks ive done this already i changed
    Java Code:
    Item newIte = new Item
                    (Integer.parseInt(txtValue.getText()), 
                    txtItemName.getText(), txtNotes.getText(), (String) choiceList.getSelectedItem (), txtFileName.getText());
    you have been very helpful thank you

Similar Threads

  1. cannot find symbol
    By GabWit in forum New To Java
    Replies: 3
    Last Post: 01-25-2009, 01:13 AM
  2. Replies: 9
    Last Post: 10-18-2008, 09:26 PM
  3. Replies: 3
    Last Post: 05-01-2008, 09:30 AM
  4. "Cannont find symbol Constructor" error
    By Welsh in forum New To Java
    Replies: 7
    Last Post: 01-25-2008, 01:12 AM
  5. Error: cannot find symbol constructor
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •