advice for an Odd, Even, Zero counter from input.txt file
Code Java:
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.
Re: advice for an Odd, Even, Zero counter from input.txt file
Hint
Code java:
int number = 81723;
System.out.println(number % 10);
System.out.println(number / 10);
System.out.println(5 % 2);
System.out.println(6 % 2);
Re: advice for an Odd, Even, Zero counter from input.txt file
hmm
u need loop for every number
3 counters, tempnumber
Code :
for (i=0;i<=number.leght;i++)
tempnumber=Integer.parseInt(number,number.charAt(i));
3 if temp==0 count 0
....
System.out.println(number,count1,count2...
}/end for
Re: advice for an Odd, Even, Zero counter from input.txt file
You are getting remainder and the quotient in the same variable that causes your program to fail each time.
Look at your code carefully and you will come to know.