Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite while loop in java

Tags:

java

I am new to Java. I tried to extract employee data from a text file and store it in a collection. I used Stringtokenizer to get the strings from the file, but in the second iteration, the while loop goes infinite; it won't come out of the 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 suggest a solution.

like image 490
Karthik.m Avatar asked Feb 11 '26 18:02

Karthik.m


2 Answers

Hmm, so what happens when count goes to 3? You don't call nextToken any more so you'll never run out of tokens.

You really don't need that inner loop. You always want to pull 2 tokens out of that string, so just do that! You may want some error handling in case a line doesn't have 2 tokens, though.

like image 76
Carl Smotricz Avatar answered Feb 14 '26 08:02

Carl Smotricz


just 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....

like image 33
Hariharbalaji Avatar answered Feb 14 '26 06:02

Hariharbalaji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!