Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: advice for an Odd, Even, Zero counter from input.txt file

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default advice for an Odd, Even, Zero counter from input.txt file

    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.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: advice for an Odd, Even, Zero counter from input.txt file

    Hint
    int number = 81723;
    System.out.println(number % 10);
    System.out.println(number / 10);
    System.out.println(5 % 2);
    System.out.println(6 % 2);

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: advice for an Odd, Even, Zero counter from input.txt file

    hmm
    u need loop for every number
    3 counters, tempnumber

    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

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.

Similar Threads

  1. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2010, 08:34 PM
  2. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 06:26 PM
  3. what happens when you supply the same name of a nonexistent input file?
    By etidd in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 28th, 2010, 11:59 PM
  4. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM
  5. Input file parsing to insert into DB
    By IDForums in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 30th, 2009, 02:29 AM