Hi... I really have a big problem in this program... I dont know how to properly read a user input from a text file.

I will really appreciate your help. thanks.

import java.util.*;
import java.io.*;

//--------------------------in this part is my problem----------------------//
public void openFile() {

File myFile = new File("students.txt");

try {
BufferedReader in = new BufferedReader(new FileReader(myFile));
String n ,i,y,c, name, id, year, course;

try {
n=in.readLine();
i=in.readLine();
y=in.readLine();
c=in.readLine();

while(n!=null && i!=null && y!=null && c!=null) {

name=n;
id=i;
year=y;
course=c;

n=in.readLine();
i=in.readLine();
y=in.readLine();
c=in.readLine();

stud.add(new Student(name,id, year, course));

n=in.readLine();
i=in.readLine();
y=in.readLine();
c=in.readLine();

}

} finally {
in.close();
}

} catch (FileNotFoundException e1) {
System.out.println("File not found: " + myFile);

} catch (IOException e2 ) {
e2.printStackTrace();
}
}

Can you explain what you are trying to do and what the problem is?

Recommendations: Don't be so lazy when typing in the names of variables. Give them names that mean something. Do Not use short names like: n, i, y, c.

name=n;
id=i;
year=y;
course=c;

Instead of reading into one variable and copying to another, read the data directly into where you want it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.