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 3 of 3

Thread: Application that determines and prints odd, even and zero digit in an integer value

  1. #1
    Junior Member
    Join Date
    Jul 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Application that determines and prints odd, even and zero digit in an integer value

    howdy,

    Another problem I've hit a wall with.

    The problem is to implement an application that determines and prints the number of odd, even, and zero digits in an integer value read from the keyboard.

    I can't think how to split the int up to check the individual values.

    Any tips?

    (I'm still new to this and haven't gone onto arrays yet, so if possible could a solution be found that is still quite basic)

    Many thanks,


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: checking individual digits of an int

    Hello Konnor,

    You can split the given int into individual values by using the java Split function.

    I will write you a quick example ASAP but for now I suggest you check Google so you can get familiar with it.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Wink Re: checking individual digits of an int

    Because the Split function needs a delimiter, and your int value wont contain one suitable I have had to use another method to split the intager after each number.

    Take a look at this code. The given int value is split after each number and put into an Array which you can then work with..

    import java.util.Scanner;
     
    public class Konnor2 {
     
        public static String stringInt;
     
        public static void main(String[] args) {
     
              System.out.println("Enter Integer Value: ");
              Scanner sc = new Scanner(System.in);
              stringInt = sc.next();
     
              String splitInt[] = new String[stringInt.length() / 1];
     
              for(int a = 0; a < splitInt.length; a++){
                  splitInt[a] = stringInt.substring(a * 1, a * 1 + 1);
                  System.out.println(splitInt[a]);
              }
        }
     
    }
    Let me know if you need help with coding the rest...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM

Tags for this Thread