• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read in file

 
Michelle Parker
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdie:

I have issues with this program:

package Scores;

import java.io.*;
import java.util.*;
import java.util.Scanner.*;


public class Scores {
public static void main(String[] args) throws Exception {
int total=0;
int count=0;
// Create a File instance
java.io.File file = new java.io.File("Scores.txt");

// Create a Scanner for the file
java.util.Scanner input = new java.util.Scanner(file);

// Read data from a file
while (input.hasNext()) {
count=count+1;
int score = input.nextInt();
total = total + score;
}
average = total / count;
System.out.println("The total = " +total +"\n"
+ "The average = " +average);

// Close the file
input.close();
}
}

With the Scores.txt containing the following:

55 88 99 50 66 31 45 15 48

Mucho Gracias!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are these issues ? Does it not compile - post the exact error messages - or does it not do what you want - tell us what it is doing and what you want it to do.
 
Michelle Parker
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joanne,

It doesn't compile at all. No error messages.
 
Michelle Parker
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need it to read in the scores from Score.txt
then give the average of them.
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No error messages? Really? Because I get some error messages that clearly specify the problem:

Where do you declare variable average?
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in the line " average = total / count;". The variable "average" has not been declared. Change that very line to "int average = total / count;", then it will compile without problems.

Maybe you might give a try to using an IDE like Eclipse, this will help you a lot in preventing such errors.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic