Compilation Error

Smock
Smock used Ask the Experts™
on
I found this code courtesy of http:
 I tried to build it but i kept getting the error below:

please help:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
      Type mismatch: cannot convert from element type Object to SearchName

Please help.
import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;

public class SearchName extends JFrame {
        static String name;
        static String age;

        public SearchName() {
        }

        public SearchName(String name, String age) {
                SearchName.name = name;
                this.age = age;
        }

        public String getName() {
                return name;
        }

        public String getAge() {
                return age;
        }

        public static void main(String[] args) {
                SearchName search = new SearchName();
                JButton button = new JButton("Search");
                JPanel panel = new JPanel();
                panel.add(button);
                search.add(panel);
                search.setSize(300, 60);
                search.setVisible(true);
                ActionListener actionListener = new ActionListener() {
                        public void actionPerformed(ActionEvent actionEvent) {
                                String n = JOptionPane.showInputDialog(null, "Enter name");
                                try {
                                        FileInputStream fstream = new FileInputStream(
                                                        "studentRecord.txt");
                                        DataInputStream in = new DataInputStream(fstream);
                                        BufferedReader br = new BufferedReader(
                                                        new InputStreamReader(in));
                                        String strLine;
                                        ArrayList list = new ArrayList();
                                        while ((strLine = br.readLine()) != null) {
                                                list.add(strLine);
                                        }
                                        Iterator itr;
                                        for (itr = list.iterator(); itr.hasNext();) {
                                                String str = itr.next().toString();
                                                String[] splitSt = str.split(" ");
                                                String id = "", name = "", age = "";
                                                for (int i = 0; i < splitSt.length; i++) {
                                                        id = splitSt[0];
                                                        name = splitSt[1];
                                                        age = splitSt[2];
                                                }
                                                ArrayList alist = new ArrayList();
                                                alist.add(new SearchName(name, age));
                                                for (SearchName s : alist) {
                                                        if (n.equals(s.getName())) {
                                                                System.out.println("Name Age ");
                                                                System.out.print(s.getName() + " " + s.getAge());
                                                        }
                                                }
                                        }
                                } catch (Exception e) {
                                }
                        }
                };

                button.addActionListener(actionListener);
        }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Make that
ArrayList<SearchName> alist = new ArrayList<SearchName>();

Open in new window

Author

Commented:
Thanks CEHJ. One more thing, I am trying to make sense of the code and in my text file,
I have:

1, John, 10.

Is that right? or is it

id = 1, name = John, age = 10
at which line? line no?

you cannot assign a value to a static variable in a constructor, since the value is already assigned to it by the time constructor is called which cannot be changed..

it should be

public SearchName(String name, String age) {
                constructor(name, age);
}

private static constructor(String name1, String age1)
{
               name = name1;
               age = age1;
}
Top Expert 2016

Commented:
Should be the below, although i don't think you should probably be constructing JFrame subclasses:
String[] splitSt = str.split("\\s*,\\s*");

Open in new window

Author

Commented:
Since this code was found online. I am trying to figure out the format that my text file should be from the code. With the text file I have right now, I am not getting any output.

Author

Commented:
Never mind. I figured it out. Thanks for your help.
Top Expert 2016

Commented:
:-)
thanks for the points

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial