Infinite loop in Java

I'm new to Java I tried to extract employee data from a text file and store it in a collection I use stringtokenizer to get strings from the file, but in the second iteration, the while loop becomes infinite; It does not appear in a while loop My code is:

public class Reader1 {
    String a;
    int i = 0;
    int count = 0;
    int x = 0;
    int y = 0;
    File f = new File(
            "C:\\Documents and Settings\\kmoorthi\\Desktop\\ak\\sample.txt");

    ArrayList<Employee> al = new ArrayList<Employee>();

    public void notePad() throws IOException {

        try {
            FileReader fis = new FileReader(f);
            BufferedReader br = new BufferedReader(fis);

            do {
                a = br.readLine();
                i++;
                if (i > 1) {
                    if (a != null) {
                        StringTokenizer st = new StringTokenizer(a,"    ");
                        Employee e = new Employee();
                        System.out.println("hai1");
                        while (st.hasMoreTokens()) // here became infinite
                        {
                            count++;
                            if (count == 1) {
                                e.ename = st.nextToken();
                                al.add(e);
                            }

                            if (count == 2) {
                                e.eno = st.nextToken();
                                al.add(e);
                            }
                        }
                    }
                }
            } while (a != null);
            br.close();

        } catch (FileNotFoundException q) {
            q.printStackTrace();
        }
    }

    public void retrieve() {
        Iterator<Employee> it = al.iterator();
        while (it.hasNext()) {
            Employee fi = (Employee) it.next();
            String en = fi.ename;
            System.out.println(en);
        }
    }

    public static void main(String s[]) throws IOException {
        Reader1 r = new Reader1();
        r.notePad();
        r.retrieve();
    }
}

Please propose a solution

Solution

Try this code

while(st.hasMoreTokens())
{

    if(count==1)
    {
        e.ename=st.nextToken();
        count++;
    }
    if(count==2)
    {
        e.eno=st.nextToken();
        count=0;
    }
    a1.add(e);
}

I think this will solve your problem

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>