Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Files in program?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41073
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Are you here to take over the surface world? Because this tiny ad will stop you!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic