aspose file tools*
The moose likes I/O and Streams and the fly likes Loading an array with data from a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Loading an array with data from a file" Watch "Loading an array with data from a file" New topic
Author

Loading an array with data from a file

Christopher Beech
Ranch Hand

Joined: Feb 08, 2006
Posts: 40
Hello, everybody. I would like to request help on two problems that I'm having. The first problem is that I need to fill an array with data gathered from a text file. The text file is a name followed by an int.

Example:

Chris 43
Bob 87
Al 45
Ken 99
Frank 95
Tony 44
Peterson 100

It should be an array of StudentRecords and does not need to be sorted in any order. Assume that the file is named file and is in the windows dirrectory. Here's what I've got so far:

private StudentRecord [] directory;
private int size = 0;

StudentDirectory(String inFileName){
this.sourceName = inFileName;
String name;
int numOfGrades;

try{
File inFile = new File("c:/windows/file.txt");
Scanner inStream = new Scanner (inFileName);
name = inStream.next ();
numOfGrades = inStream.nextInt ();

int [] grades;
grades = new int [numOfGrades];

while ((name = file.next ()) != null){
if ((grade = file.nextInt ()) == null){
break;
}
}
}
catch (IOException e){
System.out.println ("I/O ERROR");
System.exit (1);
}
}

Also, I'm unsure if the code I wrote for the save method is correct. I'm sure I can finish the rest of the assignement myself just as long as I get the StudentDirectory and save code correct. Here's what I've got for save:

void save(String outFileName){
try{
PrintWriter outfile = new PrintWriter ( new FileWriter ("C:\\in.txt"));
for (int i = 0; i < size; i++){
outfile.println(directory[i].getName());
outfile.println(directory[i].getGrade());
}
outfile.close ();
}
catch (IOException e){
System.out.println ("I/O ERROR");
System.exit (1);
}
}

Please help as soon as possible.

Thank you!!!
[ February 08, 2006: Message edited by: Christopher Beech ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42599
    
  65
Hello Christopher-

Welcome to JavaRanch.

We're not really in the business of finishing someones homework for them. So it won't do to say "here's what I got, please help me finish it". You'll need to describe what it does, and what you were actually expecting it to do. Then we'll be happy to help you get going again.
[ February 08, 2006: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Christopher Beech
Ranch Hand

Joined: Feb 08, 2006
Posts: 40
Ok. Didn't think I was asking to finish my homework for me. I left off four other methods. But I'll try this again.

The first part needs to open an input file and load the record data to the array. The input file has a name followed by a grade with a space in between (see above post for example). Needs to catch any I/O Exception if one is thrown by terminating the program by calling System.exit(1).

The save method, void save(String outFileName), needs to take an output file as its parameter and save all existing student records to the output file in the same format as an input file. Needs to catch an I/O Exception should one be thrown by terminating the program by calling System.exit(1).

I "think" that my code for the save method (see above post) is correct. Or at least I think it is.

Ok. Hope I did better this time. Never used forums for anything more than talking about video games and scifi movies.
[ February 08, 2006: Message edited by: Christopher Beech ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42599
    
  65
Now we know what the code is supposed to do. And what does it actually do? (I'm assuming that it doesn't do what it is supposed to do, otherwise you wouldn't be posting here )

Some things that strike me as probably deficient are:
- There's code to read the number of students from the file, but the file does not contain that number.
- The code only seems to read and store the grades, not the names, so it wouldn't work with the file shown above.
- In the output file, the name and grade of a student would end up each on their separate lines, not on the same line separated by a space.
Christopher Beech
Ranch Hand

Joined: Feb 08, 2006
Posts: 40
Currently, the StudentDirectory(String inFileName) method is causing the compilier to choke to death on the following lines:

this.sourceName = inFileName;

while ((name = file.next ()) != null){
if ((grade = file.nextInt ()) == null){
break;
}
}

As far as I can tell, the compilier likes my code for the save method.
Christopher Beech
Ranch Hand

Joined: Feb 08, 2006
Posts: 40
Nevermind. I fixed both methods.

Go me!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading an array with data from a file