Grades Below Average

Same problem topics merged

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1062 Views - Last Post: 13 April 2009 - 11:29 AM Rate Topic: -----

#16 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Grades Below Average

Posted 13 April 2009 - 10:39 AM

Make sure that the file that it's supposed to read from is in the same directory as the .java/.class file.
Was This Post Helpful? 0
  • +
  • -

#17 krizjaz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 99
  • Joined: 07-October 07

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

Was This Post Helpful? 1
  • +
  • -

#18 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

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?
Was This Post Helpful? 0
  • +
  • -

#19 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

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
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2