Reading from a file

Reading a list of double in

Page 1 of 1

3 Replies - 540 Views - Last Post: 08 November 2009 - 05:38 PM Rate Topic: -----

#1 illjay7005  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-November 09

Reading from a file

Post icon  Posted 08 November 2009 - 11:32 AM

public static void main(String[] args) throws Exception
	{
		File fileToRead = new File("scores.txt");
		Scanner input = new Scanner(fileToRead);

		final int NUM_STUDENTS = 16;
		String studentName;
		double labScores = 0;
		double quizScores, projectScores, examScores, finalScore;
		while (input.hasNext())
		{
			studentName = input.nextLine();
			for (int i = 0; i < 15; ++ i)
			{
				labScores = input.nextDouble();
			}
			System.out.println(labScores);
		}


hello, i am having some trouble reading some numbers from a file.
The file is set up in the below format. I need to read the lab scores (2nd line) and do an average of them. I am trying to use a for loop to do this, but it is not working and I am really confused. All I would like to do right now is figure out how to read in the whole line of lab scores, but as individual doubles. I don't know if this makes sense or not, but I would appreaciate any help!

Puckett, Karen
10 10 9.5 10 10 8 9.5 10 10 10 9 10 10 10 0
4 3 5 3 5 2 3 2 1.5 1 5 3.5
17.5 24 22 23.5 22 23
90 91
96
Little, Charles
10 10 9 10 10 4 9 10 0 10 10 10 10 10 10
2 5 3 2.5 1 4 0 4 5 4 3.5 5
22 7 19.5 23 24 20.5
78 56
53... and so on

This post has been edited by illjay7005: 08 November 2009 - 12:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading from a file

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Reading from a file

Posted 08 November 2009 - 02:04 PM

If you need it to be read in line by line. Read in the line of code that has the numbers and save as a String, then create a StringTokenizer with the delimiter of ' ' and make a while loop to read each token, which will be each grade that you can add to an array ect.
Then read in the next line and so he same to seperate the grades and you can do so until you have all of them. Create the code to do it for one person, once it works successfully for one person, just make all of that code inside a while loop with condition of hasNextLine() to cycle through the entire txt file.

Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,515
  • Joined: 20-September 08

Re: Reading from a file

Posted 08 November 2009 - 04:22 PM

Use a Scanner. Start with s.next() to get your name and then do

while (s.hasNextDouble()) {
	scores.add(s.nextDouble());
}


for each student
Was This Post Helpful? 0
  • +
  • -

#4 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Reading from a file

Posted 08 November 2009 - 05:38 PM

class Scanner works on an input Stream and class File is not an input Stream so you may use FileReader instead of File
FileReader fr=new FileReader("scores.txt");
Scanner s=new Scanner(fr);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1