Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Files in program?

 
Sally Curtis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic