Complete Question Text: Write a method called evenNumbers that accepts a Scanner
ID: 3766823 • Letter: C
Question
Complete Question Text:
Write a method called evenNumbers that accepts a Scanner reading input from a file with a series of integers, and report various statistics about the integers to the console. Report the total number of numbers, the sum of the numbers, the count of even numbers and the percent of even numbers. For example, if the input file contains the following text:
5 7 2 8 9 10 12 98 7 14 20 22
Then the method should produce the following console output:
12 numbers, sum = 214
8 even (66.67%)
Explanation / Answer
import java.io.*;
import java.util.*;
class file_read_tot_sum
{
public static void evenNumber()
{
File file = new File("10_Random");
int sum=0,e=0,i,c=0;
try
{
Scanner sc = new Scanner
(file);
while (sc.hasNextLine())
{
i = sc.nextInt();
c++;
sum=sum+i;
if(i%2==0)
e++;
}
System.out.println(c+"
number, sum ="+sum+" "+e + "even");
}
catch (FileNotFoundException ee)
{
ee.printStackTrace();
}
}
public static void main(String args[])
{
evenNumber();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.