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.