Need advice for an odds, even, zero counter from input.txt file.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2536 Views - Last Post: 29 March 2011 - 06:20 AM Rate Topic: -----

#16 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Need advice for an odds, even, zero counter from input.txt file.

Posted 28 March 2011 - 06:29 PM

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?
Was This Post Helpful? 1
  • +
  • -

#17 sanchezjk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 02-February 11

Re: Need advice for an odds, even, zero counter from input.txt file.

Posted 28 March 2011 - 10:39 PM

View Postr.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?


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.
:^: :^: :^: :^: :^:
Was This Post Helpful? 0
  • +
  • -

#18 Greltam   User is offline

  • D.I.C Head

Reputation: 91
  • View blog
  • Posts: 225
  • Joined: 29-January 09

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..
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;
}


Was This Post Helpful? 0
  • +
  • -

#19 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Need advice for an odds, even, zero counter from input.txt file.

Posted 29 March 2011 - 06:20 AM

View PostGreltam, on 29 March 2011 - 05:24 AM, said:

I think you have program logic wrong..
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

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2