The document describes two Java programs:
1. A method called evenNumbers that accepts a Scanner reading integers from a file, counts the total numbers, sum, even numbers, and percentage of evens.
2. A method called collapseSpaces that accepts a Scanner for an input file, reads it and outputs the tokens separated by single spaces, collapsing multiple spaces.
#write java programWrite a method called evenNumbers that accepts .docx
1. #write java program
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
6 evens (66.67%)
-- although you may format your output differently. Please
round your percentage output to 2 decimal places.
Problem 5:
Write a method called collapseSpaces that accepts a Scanner
representing an input file as its parameter, then reads that file
and outputs it with all its tokens separated by single spaces,
collapsing any sequences of multiple spaces into single spaces.
For example, consider the following text:
many spaces on this line!
If this text were the only line in the file, the same line should be
output as follows:
many spaces on this line!
For this problem, new line characters should also be counted as
spaces.
You should create sample text files to test your programs. These
text files will need to be imported into Eclipse (it will be placed
under the JRE System Library). You will then be able to specify
just the name of the file (relative path), rather than the absolute
path.
2. Solution
/* 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.
*/
import java.util.*;
import java.io.*;
public class Check_evenNumbers{
public static void main (String[] args) throws
FileNotFoundException{
Scanner input = new Scanner(new File("integers.txt"));
evenNumbers(input);
}
public static void evenNumbers( Scanner inp){
int count = 0;
int sum = 0;
int evens = 0;
while(inp.hasNextInt()){
3. int num = inp.nextInt();
count++;
sum += num;
if (num % 2 == 0){
evens++;
}
}
double percent = evens/count * 100;
System.out.println(count + " numbers, sum = " + sum);
System.out.println(evens + " evens (" + percent + "%)");
}
}
/*
* method called collapseSpaces that accepts
* a Scanner representing an input file as its parameter,
* then reads that file and outputs it with all its tokens
* separated by single spaces, collapsing any sequences
* of multiple spaces into single spaces.
*/
import java.util.*;
import java.io.*;
public class Check_collapseSpaces{
public static void main (String[] args) throws
FileNotFoundException{
Scanner fileInput = new Scanner(new File
4. ("text_with_spaces.txt"));
// read in the line so we can print it before changing it
String line = fileInput.nextLine();
System.out.println(line);
Scanner lineInput = new Scanner(line);
collapseSpaces(lineInput);
}
public static void collapseSpaces(Scanner line){
while (line.hasNext()){
String token = line.next();
System.out.print(token + " ");
}
System.out.println();
}
}