Heres my code:
import java.util.*;
import java.io.*;
public class Ch7Ex11 {
public static final int N = 10;
@SuppressWarnings("unused")
private static final int getNumber = 0;
public static void main(String[] args) throws FileNotFoundException {
int sumNumbers = 0;
int totalNumbers = 0;
IntClass oddCounter = new IntClass(), evenCounter = new IntClass(), zeroCounter = new IntClass();
Scanner input = new Scanner(new FileReader("F:\\Ch7_Ex11Data.txt"));
PrintWriter output = new PrintWriter("F:\\classifynumbersoutput.txt");
while (input.hasNext()) {
int number = getNumber(input, output);
sumNumbers += number;
totalNumbers++;
if (totalNumbers % N == 0) {
System.out.println();
output.println();
}
classifyNumber(number, zeroCounter, evenCounter, oddCounter);
}
printResult(output, totalNumbers, sumNumbers, evenCounter, zeroCounter,
oddCounter);
input.close();
output.close();
}
private static int getNumber(Scanner input, PrintWriter output) {
int anInt = input.nextInt();
System.out.println(anInt);
output.println(anInt);
return anInt;
}
private static void classifyNumber(int number, IntClass oddCounter,
IntClass zeroCounter, IntClass evenCounter) {
switch (number % 2) {
case 0:
evenCounter.addToNum(1);
if (number == 0)
zeroCounter.addToNum(1);
;
break;
case 1:
case -1:
oddCounter.addToNum(1);
}
}
private static void printResult(PrintWriter output, int totalNumbers,
int sumNumbers, IntClass zeroCounter, IntClass oddCounter,
IntClass evenCounter) {
System.out.println(" " + totalNumbers + " numbers were read.");
System.out.println(" " + sumNumbers + " is total sum.");
System.out.println(" " + sumNumbers / totalNumbers + " is the average.");
System.out.println("Total numbers of odds: " + oddCounter);
System.out.println("There are " + zeroCounter + " Zeros.");
System.out.println("Total evens are: " + evenCounter);
output.println(" " + totalNumbers + " numbers were read.");
output.println(" " + sumNumbers + " is total sum.");
output.println(" " + sumNumbers / totalNumbers + " is the average.");
output.println("Total numbers of odds: " + oddCounter);
output.println("There are " + zeroCounter + " Zeros.");
output.println("Total evens are: " + evenCounter);
}
}
The program accepts numbers from a file of unknown length and then is supposed to print them 10 numbers a line at a time. The program then is supposed to print how many numbers there are, the sum, the average, the total number of odds, the total number of evens, and the total number of zeros. I got almost everything to work, but I am having trouble with the ouput. How do I make it print only 10 numbers per line instead of printing like this:
1
2
3
4
5
6
7
8
9
10
1
2
3
...
ect. (these are fake inputs by the way)
Thanks! Your help is greatly appreciated!