Page 1 of 1

## 3 Replies - 873 Views - Last Post: 08 November 2009 - 05:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=137537&amp;s=61343be52ca0d248b842b09437b09b17&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 illjay7005

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

Posted 08 November 2009 - 11:32 AM

```public static void main(String[] args) throws Exception
{

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

• Comp Sci Student

Reputation: 669
• 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!

### #3 g00se

• D.I.C Lover

Reputation: 3562
• Posts: 16,235
• Joined: 20-September 08

## Re: Reading from a file

Posted 08 November 2009 - 04:22 PM

```while (s.hasNextDouble()) {
}

```

for each student

### #4 sakshamkum

Reputation: 19
• 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);

```