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

Thread: Help with logic to find the day number of the year

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help with logic to find the day number of the year

    Hello, I need help with figuring out the logic to print the day number of the year. The directions for my homework are: Write a program that prints the day number of the year, given the date is in the form: month day year. For example, if the input is 1 1 05, the day number is 1; if the input is 12 25 05, the day number is 359. The program should check for a leap year . I have a basic outline of the program but i am having trouble with figuring out the logic to display the day number of the year given the date. This is what i have so far

    import javax.swing.JOptionPane;
     
    public class Practice4
    {
    	public static void main(String[] args)
    	{
    	    String monthStr;
    	    String dayStr;
    	    int inputMonth;
    	    int inputDay;
    	    String yearStr2;
    	    String dayStr2;
        	int inputyear2;
            int inputDay2;
     
        	String[] Months = {"January", "February", "March", "April", "May",
                			   "June", "July", "August", "September", "October", "November",
                			   "December"};
     
        	int[] Days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
     
        	monthStr = JOptionPane.showInputDialog(null, "Enter the Month: ");
        	inputMonth = Integer.parseInt(monthStr);
     
        	dayStr = JOptionPane.showInputDialog(null, "Enter the Day: ");
        	inputDay = Integer.parseInt(dayStr);
     
        	yearStr2 = JOptionPane.showInputDialog(null, "Enter the year: ");
        	inputyear2 = Integer.parseInt(yearStr2);
     
        	JOptionPane.showMessageDialog(null, );//To display the day number of the year
     
        	public static int dayNumber(int dayCount)//Method for logic to find day number
        	{
    			for( )//Need help figuring out logic to print day number of the year
            	{
              	boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)||(year % 400 == 0); //accounting for leap year(probably doesnt go here)
            	}
     
        	}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,137
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with logic to find the day number of the year

    What do you have so far for the logic?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with logic to find the day number of the year

    Norm, I dont have anything as of yet... I think I need to assign the date to a number then keep a running count until December 31st. I am unsure of how to just single out the month and day from the year though.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,137
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with logic to find the day number of the year

    how to just single out the month and day from the year though.
    What format are they in when the program reads them?
    The examples you gave were numeric digits separated by spaces.
    One way to read that would be to use the Scanner class's nextInt method three times to read in the numbers and assign them to variables.

    But that has nothing to do with the logic of the program. You can assume that you have the three values in variables for now. Later you can work out how to read them.
    Now: what does the program need to do to solve the problem given those 3 numbers?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with logic to find the day number of the year

    Right I could use the Scanner class and use nextInt method 3 times and assign them to a variable. I will definitely do that. The program needs to read those numbers and keep a running total of the day of the year it is and account for a leap year if it is inputted by the user.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,137
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with logic to find the day number of the year

    keep a running total of the day of the year it is
    and account for a leap year
    Do you have the logic now? What you've posted is a bit high level. Have you worked out the details for doing the details for those steps?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with logic to find the day number of the year

    What do you mean by high level?... I have not worked out the "details" do I make some sort of loop? maybe a for loop or should i use the switch method? and if i use a for loop how would I get a counter to count the days from what the user inputs?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,137
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help with logic to find the day number of the year

    By details I mean, what will the code do with the three numbers it gets from the user?
    How would you solve the problem if you did it using paper and pencil?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Find the first day of any year
    By avani in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 25th, 2014, 08:49 AM
  2. Replies: 1
    Last Post: March 21st, 2013, 06:57 AM
  3. return a day of the year
    By lipry in forum Java Theory & Questions
    Replies: 3
    Last Post: January 23rd, 2011, 04:10 PM
  4. Day,Month,Year Concatenation Error
    By srinivasan_253642 in forum JDBC & Databases
    Replies: 1
    Last Post: January 22nd, 2010, 03:40 AM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM