OK, you're working with extraNum. Now you want to "slice off" its digits one by one until there are none left, and examining each one to classify it as you go along.
So if extraNum = 1234
then extraNum % 10 = 4 (is that even, odd or zero?)
and extraNum / 10 = 123
and you should be able to figure out when none are left
Now can you put that in a loop?
18 Replies - 2536 Views - Last Post: 29 March 2011 - 06:20 AM
#16
Re: Need advice for an odds, even, zero counter from input.txt file.
Posted 28 March 2011 - 06:29 PM
#17
Re: Need advice for an odds, even, zero counter from input.txt file.
Posted 28 March 2011 - 10:39 PM
r.stiltskin, on 28 March 2011 - 06:29 PM, said:
OK, you're working with extraNum. Now you want to "slice off" its digits one by one until there are none left, and examining each one to classify it as you go along.
So if extraNum = 1234
then extraNum % 10 = 4 (is that even, odd or zero?)
and extraNum / 10 = 123
and you should be able to figure out when none are left
Now can you put that in a loop?
So if extraNum = 1234
then extraNum % 10 = 4 (is that even, odd or zero?)
and extraNum / 10 = 123
and you should be able to figure out when none are left
Now can you put that in a loop?
Okay. After help from you and Greltam, I finally figured it out. I just wanted to properly thank you for helping me out. Thanks for not just giving me code and teaching it to me the proper way. It took me quite a while to figure it out, but here is what I got:
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;
int lastNum = 0;
tempLine = fileScan.nextLine();
int num = Integer.parseInt(tempLine);
int extraNum = num;
do {
lastNum = extraNum % 10;
if (lastNum == 0) {
zeroCount++;
} else if (lastNum % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
extraNum /= 10;
} while (extraNum > 0);
System.out.println("Number " + num + " has " + oddCount
+ " odds, " + evenCount + " evens, " + zeroCount
+ " zero digits. ");
}
}
}
I took your advice with the two new variables and it really helped simplify it a lot. My output is correct and I understand the coding 100%. Thanks again for all your help.
#18
Re: Need advice for an odds, even, zero counter from input.txt file.
Posted 29 March 2011 - 03:24 AM
I think you have program logic wrong..
Before you even compare your number you're setting it to 0; I would suggest doing another temp int that gets the last digit, and then modulo that;
int extraNum = num; // here is the extra variable so I can keep the value of num unchanged.
extraNum = extraNum % 10; // <--------extraNum is now 0 - 9
extraNum = extraNum /= 10; // <-------extraNum is now probably 0;
Before you even compare your number you're setting it to 0; I would suggest doing another temp int that gets the last digit, and then modulo that;
while(extraNum > 0)
{
int singleDigit = extraNum % 10;
//is singleDigit odd/even/zero?
extraNum /= 10;
}
#19
Re: Need advice for an odds, even, zero counter from input.txt file.
Posted 29 March 2011 - 06:20 AM
Greltam, on 29 March 2011 - 05:24 AM, said:
I think you have program logic wrong..
Before you even compare your number you're setting it to 0; I would suggest doing another temp int that gets the last digit, and then modulo that;
int extraNum = num; // here is the extra variable so I can keep the value of num unchanged.
extraNum = extraNum % 10; // <--------extraNum is now 0 - 9
extraNum = extraNum /= 10; // <-------extraNum is now probably 0;
Before you even compare your number you're setting it to 0; I would suggest doing another temp int that gets the last digit, and then modulo that;
while(extraNum > 0)
{
int singleDigit = extraNum % 10;
//is singleDigit odd/even/zero?
extraNum /= 10;
}
You're a couple of posts behind. The OP's corrected code is in Post #17.
This post has been edited by r.stiltskin: 29 March 2011 - 06:20 AM

New Topic/Question
Reply



MultiQuote



|