File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Files in program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Files in program?" Watch "Files in program?" New topic
Author

Files in program?

Sally Curtis
Greenhorn

Joined: Jan 10, 2004
Posts: 26
hello,
i was given an assigment to write a program that asks for the name of a roster file that has students names in it. Then the program promts for eacht students grade and with those grades does some calculations. At the end a table is outputted with the name of the student and the calculations. the program is include an array. My question is, how do i know how many students are in the file? is there some kind of method that counts the lines of the text file?
this is my first few lines of the program

String DataFileName="";

DataFileName=keyb.askString("What is the name of your roster?");
File inFile=new File(DataFileName);



that last line, does that read the file or just creates a new File instance? and should my array in my program consist of the students names or there grades?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
For the file reading and writing quesions, I'm moving this to the I/O and Streams forum. If you've further questions on designing your program, I suggest starting a new thread for them.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

Sally,
You need to write a method that reads in each line and counts how many there are. Java doesn't have a pre-written method for that.

In the last line, the File object is a pointer to a file. (The file doesn't even have to exist.) You need to open the file for reading separately.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I wonder if "array" is part of the assignment. I'm guessing you want the number of students so you can allocate the array to the right size, and maybe again for the calculations - eg average or something.

There is a way around the array size problem, and that is to use a List. If you look up List in the JavaDoc you'll find it is an interface and there are several classes that implement List. You might do something like:

If you really have to have an array, you can get an array from the List. Look at List APIs again for hints.

Take small steps - make one thing work at a time. Feel free to post code as you go if you run into problems. Have fun!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Files in program?