import java.util.Scanner; import java.io.*; public class assignment4_question3 { public static void main(String[] args) throws IOException { String tempLine = "input.txt"; Scanner fileScan; fileScan = new Scanner(new File("input.txt")); while (fileScan.hasNextLine()) { int evenCount = 0; int oddCount = 0; int zeroCount = 0; tempLine = fileScan.nextLine(); int num = Integer.parseInt(tempLine); int extraNum = num; extraNum = extraNum % 10; extraNum = extraNum /= 10; if (extraNum == 0) { zeroCount++; } while (extraNum > 0) { if (extraNum % 2 == 2) { evenCount++; } else { oddCount++; } } System.out.println("Number " + num + " has " + oddCount + " odds, " + evenCount + " evens, " + zeroCount + " zero digits. "); } } }
Okay. This is my code so far.
These are the instructions:
Design and implement a program that read a series of integer numbers from the input file input.txt, determines and prints the number of odd, even, and zero digits in each integer value.
The input.txt contains this:
1234
5008
3245356
352665
2334546
Now. I need to figure out a way to count how many odd, even, and zero digits each of these contains
Here is the correct output:
Number 1234 has 2 odd, 2 even, 0 zero digits.
Number 5008 has 1 odd, 1 even, 2 zero digits.
Number 3245356 has 4 odd, 3 even, 0 zero digits.
Number 352665 has 3 odd, 3 even, 0 zero digits.
Number 2334546 has 3 odd, 4 even, 0 zero digits.
But this is my output [it is horribly wrong and I can't seem to figure out why]:
Number 1234 has 0 odds, 0 evens, 1 zero digits.
Number 5008 has 0 odds, 0 evens, 1 zero digits.
Number 3245356 has 0 odds, 0 evens, 1 zero digits.
Number 352665 has 0 odds, 0 evens, 1 zero digits.
Number 2334546 has 0 odds, 0 evens, 1 zero digits.
Any advice or direction would be unbelievably appreciated.