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();
}
}
TopTenListclass orJFramewhich it inherits from has anadd()method that takes those arguments so ... yes, that would be the correct error. It's also unclear what you expect that would do.