0

I am in the middle of a homework assignment and I am stuck. At this point in my code I think I should have a gui window that opens and allows me to type "insert text number". At this point that information is not going anywhere but it will be going into a linkedlist once I get past this problem. I am getting two of the same error for the lines tt.add(index, element); and I can't seem to get past it. The error is "no suitable method found for add(int, java.lang.String)". Code is below, please advise. To clarify - this should NOT be a method error since this is a linked list. No method should be involved.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.Scanner;
import java.util.LinkedList;

public class TopTenList extends JFrame
{
private TopTenList tt;
private JTextArea listView;
private JTextField cmdTextField;
private JTextField resultTextField;

// This is the code for the GUI Window
public TopTenList()
{
    tt = new TopTenList();
    listView = new JTextArea();
    cmdTextField = new JTextField();


    //Create panel and label for the command text field
    JPanel cmdPanel = new JPanel(new GridLayout(1,2));
    cmdPanel.add(new JLabel ("Enter New Score: "));
    cmdPanel.add(cmdTextField);
    add(cmdPanel, BorderLayout.NORTH);
    cmdTextField.addActionListener(new CmdTextListener());

    // Set up the frame
    setTitle("Top Ten Scoreholders");  // Window Title
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Behavior on close
    pack();
    setVisible(true);  // Display the window


    // Put the textArea in the center of the frame
    add(listView);
    listView.setEditable(false);
    listView.setBackground(Color.WHITE);
}

// Private class that responds to the new score entered by the user
private class CmdTextListener implements ActionListener
{
    public void actionPerformed(ActionEvent evt)
    {
        String cmdText = cmdTextField.getText();
        Scanner sc = new Scanner(cmdText);
        String cmd = sc.next();
        if (cmd.equals("insert"))
        {
            if(sc.hasNextInt())
            {
                // add index element
                int index=sc.nextInt();
                String element = sc.next();
                tt.add(index, element);

            }
            else
            {
                // add element
                String element = sc.next();
                tt.add(element);
            }
            listView.setText(tt.toString());
            pack();
            return;
        }
    }
}


   // The main method to run the top ten list

 public static void main(String [ ] args)
{
    new TopTenList();
} 
}
5
  • 1
    The error tells you pretty literally what the problem is. Commented Apr 24, 2014 at 2:54
  • Well, neither your TopTenList class or JFrame which it inherits from has an add() method that takes those arguments so ... yes, that would be the correct error. It's also unclear what you expect that would do. Commented Apr 24, 2014 at 2:58
  • Are you trying to display some text to your window? Commented Apr 24, 2014 at 3:00
  • The text that will eventually be displayed in the window will be a list of 10 items, a line that says "Enter New Score: ", and a text box for entering a command (insert right now), text and a score. Personally I would prefer to break these into separate text boxes for error checking but that is not the assignment. Commented Apr 24, 2014 at 3:49
  • The question states "To clarify - this should NOT be a method error since this is a linked list. No method should be involved." There is no LinkedList in that code. There is an import for java.util.LinkedList but I don't see that every being used. Commented Apr 25, 2014 at 16:00

2 Answers 2

1

You never made an add(int i, Object o) method in either of your classes. You're treating tt like a List when it isn't so you would need to make the method or you could try implementing the List class.

Sign up to request clarification or add additional context in comments.

1 Comment

I thought that I was implementing the List class which is why the method error has been driving me batty. How do I implement the list? I thought that I did that with private TopTenList tt;
0

The various add() methods that your TopTenList is inheriting from JFrame, and indirectly from java.awt.Container and java.awt.Component, do not provide for adding String objects, just other Component objects.

I think maybe you instead want a javax.swing.JList with a javax.swing.DefaultListModel, as the DefaultListModel.add() method would take the parameters you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.