Make sure that the file that it's supposed to read from is in the same directory as the .java/.class file.
Grades Below AverageSame problem topics merged
18 Replies - 927 Views - Last Post: 13 April 2009 - 11:29 AM
#17
Re: Grades Below Average
Posted 13 April 2009 - 10:45 AM
import java.util.*;
import java.io.*;
public class SumFile
{
public static void main(String[] args) throws Exception
{
int sum = 0;
try{
Scanner fileInput = new Scanner(new File("file1.txt"));
while(fileInput.hasNextInt())
sum = sum + fileInput.nextInt();
System.out.println("Sum of integers = " + sum);
}catch(Exception ex){
System.out.println("*File Not Found*");
}
}
}
Try this code. Hope this would help.
The .txt file, or the file for input is to be with this class file's directory.
This post has been edited by krizjaz: 13 April 2009 - 10:55 AM
#18
Re: Grades Below Average
Posted 13 April 2009 - 11:24 AM
why would you compile it as a txt file? not really sure what you mean by that.... what does file1.txt look like?
#19
Re: Grades Below Average
Posted 13 April 2009 - 11:29 AM
Yes, you should compile your java file like normal, just make sure to have your file2.txt in the same directory as your program.
This program shouldn't be too hard to write. Just get the first integer and store it. Then get all the other integers and compare it to the first. Keep a count of how many grades are lower than the average and then print this out. Try and write some code to do that and we will help you if you get stuck.
-Michael
This program shouldn't be too hard to write. Just get the first integer and store it. Then get all the other integers and compare it to the first. Keep a count of how many grades are lower than the average and then print this out. Try and write some code to do that and we will help you if you get stuck.
-Michael
|
|

New Topic/Question
Reply





MultiQuote




|