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

• April 15th, 2014, 07:09 PM
brobertson300
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

Code java:

```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) }   } } }```
• April 15th, 2014, 07:26 PM
Norm
Re: Help with logic to find the day number of the year
What do you have so far for the logic?
• April 15th, 2014, 09:18 PM
brobertson300
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.
• April 15th, 2014, 09:23 PM
Norm
Re: Help with logic to find the day number of the year
Quote:

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?
• April 15th, 2014, 09:34 PM
brobertson300
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.
• April 15th, 2014, 09:43 PM
Norm
Re: Help with logic to find the day number of the year
Quote:

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?
• April 15th, 2014, 10:19 PM
brobertson300
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?
• April 16th, 2014, 07:14 AM
Norm
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?