954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Displaying user input from text file

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(); } }

Attachments StudentFiles.txt (10.7KB)
yurixzach
Newbie Poster
3 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: