Endless while loop in java

I am new to Java. I tried to extract employee data from a text file and save it in a collection. I used Stringtokenizer to get strings from a file, but in the second iteration, the while loop is infinite; it will not exit 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.

+3
source share
7 answers

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

+2
source

Hmm, so what happens when countmoving to 3? You no longer call nextTokenso that you never run out of tokens.

. 2 , ! , , 2 .

+9

, nextToken , 3. , , - count = 0; if (count == 2).

( )

= 0; . , st.nextToken(), 2.

, , , ,

e.name = line.subString(0, line.indexOf("    "));
e.no = line.subString(line.indexOf("    ") + 4);

, . .

, , , , -

count = 0;
while (st.hasMoreTokens())
{
    if (count == 0)
    {
        e.name = st.nextToken();
    }
    if (count == 1)
    {
        e.n0 = st.nextToken();
    }
    st.nextToken();
    count++;
}
al.add(e);

while (, Employee while)

e.name = st.nextToken();
e.n0 = st.nextToken();
al.add(e);
+2

, . , count == 2 3. count == 3, .

+1

, , count == 3 . st.nextToken(), 2. , nextToken(), st.hasMoreTokens() true , .

0
source

Your loop will probably hang up for any line with more than two tokens.

In this case, you should exit the loop. For example.

while(st.hasMoreTokens() && count < 2)
{
    count++;
    if(count==1)
    {
        e.ename=st.nextToken();
        al.add(e);
    }
    if(count==2)
    {
        e.eno=st.nextToken();
        al.add(e);
    }
}
0
source

Performing this through my head, the only thing that looks like this can cause an infinite loop somewhere in this loop if there are more than two entries in this line. When the counter is less than 0 or greater than 1 at the beginning of the iteration of the cycle, tokens are not consumed (via nextToken ()).

Perhaps it would be better to use all the tokens in the array and then save only the ones you care about.

0
source

Source: https://habr.com/ru/post/1723434/


All Articles