Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


kitty
kittz
1 Answer(s)      3 years and 8 months ago
Posted in : Java Beginners


here is a code it is giving error in this part using netbeans incompatible types required: word.SearchName (which is my class name) found : java .lang.Object

for (SearchName s : alist) {
                                if (n.equals(s.getName())) {
                                    System.out.println("Name Age ");
                                    System.out  .print(s.getName() + " " + s.getAge());
                                }









import java.io.*;
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.BufferedReader;
    //import java.io.FileReader;
    public class SearchName extends JFrame
    {        static String name;
             static String age;
             public SearchName()
             {        }
             public SearchName(String name, String age) {
                 this.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));


>   { here it is givig error incopatible types i am using netbeans}

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);
             }}

Advertisement
View Answers

March 11, 2011 at 4:05 PM


Try this:

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) {
             this.name = name;
             this.age = age;        }
         public String getName()
         {
             return name;        }
         public String getAge()
         {                return age;        }
         public static void main(String[] args)
         {          Scanner input=new  Scanner(System.in);
             System.out.print("Enter Name: ");
                    String n = input.next();
                    try {
                        FileInputStream fstream = new FileInputStream  ("C:/names.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 name = "", age = "";
                            for (int i = 0; i < splitSt.length; i++)
                            {
                                name = splitSt[0];
                                age = splitSt[1];
                            }
                            ArrayList<SearchName> alist = new ArrayList<SearchName>();
                            alist.add(new SearchName(name, age));
                            for (SearchName s : alist) {
                            if (n.equals(s.getName())) {
                               System.out.print(s.getName() + " " + s.getAge());
                            }
                                }
                                    }
                            } catch (Exception e) {
                            }
                    }
           }


Related Tutorials/Questions & Answers:
 

 

 

DMCA.com