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.

Page 2 of 2 FirstFirst 12
Results 26 to 28 of 28

Thread: dateOfBirth problems

  1. #26
    Junior Member
    Join Date
    Nov 2010
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: dateOfBirth problems

    tried that before, got an error for the dob.getDay etc. And I NEED the dob. as in dateOfBirth = new Date(dob)

  2. #27
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Cool Re: dateOfBirth problems

    Talking of BlueJ, does anyone else have trouble with this IDE and Windows 7 Starter Edition (32-bit)? It came pre-installed on my Netbook which I purchased for College. Oddly, I think I'm doing the same or a similar course to ibby50.

    Thanks to everyone for the suggestions in this thread, by the way. It's been most helpful for me too :-P

    Regards,

    Shaun.

  3. #28
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Cool Re: dateOfBirth problems

    Well, I've finished the assignment and handed it in (the same one that ibby50 has just finished, I think), and it seems to show the wrong way to do certain things. If you did things the right way then you'd have got no marks for it even though it would have been more efficient.

    Anyway, having a look-up table is a quick and easy way to convert a month integer (ie, value of 1 - 12) to a type String. Here's a quick and dirty example
    // Right, let's import the Scanner
    import java.util.Scanner;
    // Our class name
    class MonthToString
    {
        // This is our main proggy
        public static void main(String args[])
        {
            // Here is our matrix array, which we're using as a look-up table
            // note: the array starts at zero, so zero is January, one is February etc...
            // You'll see the significant of this later.
            String monthNames[]=
            {"January","February","March","April","May","June","July",
              "August","September","October","November","December"};
            // I like buffers, though this it not entirely necessary.
            String buff="";
            int month=0;
            // Here's our loop marker:
            for (int i=0; i<12; i=i+1)
            {
                System.out.println("Please enter month as MM (ie, 01 is January, 05 is May and 11 is November etc...)");
                System.out.print("? ");
                // Let's read the keyboard entry:
                Scanner z=new Scanner(System.in);
                buff=z.nextLine();
                // Now, let's take the literal value of the keyboard entry and convert it into a string.
                // Note the substring: you have to enter 01, 02, 03 etc... rather than 1, 2, 3 for this
                // example, though it's not entirely necessary. The one benefit is it can eleminate keyboard
                // typos in certain circumstances.
                month=Integer.parseInt(buff.substring(0,2));
                // Now we'll check if you've entered a legal month value and...
                if(month>0 && month<13)
                {
                    // ...because the array starts at zero, we need to take one away from month to output
                    // the correct month:
                    System.out.println(month+" is: "+monthNames[month-1]);
                }
                else
                {
                    // Okay, you've entered a invalid number, dun dun dunnnnn!!!!
                    System.out.println("Not a valid entry");
                }
            }
        }
    }
    Simples? Not according to my College lecturer (or at least the one who sets the assignments). You could use the same logic and simply do something like: return monthNames[month-1] if needed.

    Regards,

    Shaun.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. 2 problems...
    By Day2Day in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2010, 02:51 PM
  2. [SOLVED] Have a few odd problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: October 19th, 2010, 07:08 PM
  3. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 08:25 PM
  4. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 10:33 PM
  5. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 11:49 AM