Day of the year Program Question

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 7215 Views - Last Post: 12 June 2012 - 03:35 PM Rate Topic: -----

#1 portman1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-April 12

Day of the year Program Question

Posted 11 June 2012 - 09:53 PM

My program asks the user to enter a numbered day of the year 1-365 and my code is supposed to tell them which month and day of that month it is.
ie. 357 = December 23rd

Requirements:
To get the number from the user, use JOptionPane.showInputDialog() as described in section 2.5 of the textbook (Chapter 2s Graphics Supplement).
Use an if-else structure to print the correct month name.
Use a switch statement to print the ordinal indicator (st, nd, rd, or th).
You may assume that you will only receive an integer number between 1 and 365.


what I have thus far..

package program1;

import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class Program1 extends JFrame {

    private static final int LAST_DAY_OF_JANUARY = 31;
    private static final int LAST_DAY_OF_FEBRUARY = LAST_DAY_OF_JANUARY + 28;
    private static final int LAST_DAY_OF_MARCH = LAST_DAY_OF_FEBRUARY + 31;
    private static final int LAST_DAY_OF_APRIL = LAST_DAY_OF_MARCH + 30;
    private static final int LAST_DAY_OF_MAY = LAST_DAY_OF_APRIL + 31;
    private static final int LAST_DAY_OF_JUNE = LAST_DAY_OF_MAY + 30;
    private static final int LAST_DAY_OF_JULY = LAST_DAY_OF_JUNE + 31;
    private static final int LAST_DAY_OF_AUGUST = LAST_DAY_OF_JULY + 31;
    private static final int LAST_DAY_OF_SEPTEMBER = LAST_DAY_OF_AUGUST + 30;
    private static final int LAST_DAY_OF_OCTOBER = LAST_DAY_OF_SEPTEMBER + 31;
    private static final int LAST_DAY_OF_NOVEMBER = LAST_DAY_OF_OCTOBER + 30;
    private static final int LAST_DAY_OF_DECEMBER = LAST_DAY_OF_NOVEMBER + 31;

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        JOptionPane.showInputDialog("Enter the day of the year (1..365)");
        int input = keyboard.nextInt();
        String date;
        if (input <= LAST_DAY_OF_JANUARY) {
            date = "January";
        } else if (input > LAST_DAY_OF_JANUARY && input < LAST_DAY_OF_MARCH) {
            date = "February";
        } else if (input > LAST_DAY_OF_MARCH && input < LAST_DAY_OF_APRIL) {
            date = "March";
        } else if (input > LAST_DAY_OF_APRIL && input < LAST_DAY_OF_MAY) {
            date = "April";
        } else if (input > LAST_DAY_OF_MAY && input < LAST_DAY_OF_JUNE) {
            date = "May";
        } else if (input > LAST_DAY_OF_JUNE && input < LAST_DAY_OF_JULY) {
            date = "June";
        } else if (input > LAST_DAY_OF_JULY && input < LAST_DAY_OF_AUGUST) {
            date = "July";
        } else if (input > LAST_DAY_OF_AUGUST && input < LAST_DAY_OF_SEPTEMBER) {
            date = "August";
        } else if (input > LAST_DAY_OF_SEPTEMBER && input < LAST_DAY_OF_OCTOBER) {
            date = "September";
        } else if (input > LAST_DAY_OF_OCTOBER && input < LAST_DAY_OF_NOVEMBER) {
            date = "October";
        } else if (input > LAST_DAY_OF_NOVEMBER && input < LAST_DAY_OF_DECEMBER) {
            date = "November";
        } else {
            date = "December";
        }

        System.out.println(date);
    }
}



I'm not sure why it wont choose a month and print it and also i'm not sure how to get the exact date number.

Is This A Good Question/Topic? 0
  • +

Replies To: Day of the year Program Question

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2134
  • View blog
  • Posts: 3,274
  • Joined: 21-June 11

Re: Day of the year Program Question

Posted 12 June 2012 - 12:00 AM

View Postportman1, on 12 June 2012 - 06:53 AM, said:

I'm not sure why it wont choose a month and print it


Then what does it do instead?

Edit: Oh, I get it. You ask for input twice (once via swing, once via the console), but you're probably only entering it once.

This post has been edited by sepp2k: 12 June 2012 - 12:02 AM

Was This Post Helpful? 1
  • +
  • -

#3 portman1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-April 12

Re: Day of the year Program Question

Posted 12 June 2012 - 06:36 AM

The joption box asks them for the number and it should choose based on my boolean operands and nothing is coming up.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2134
  • View blog
  • Posts: 3,274
  • Joined: 21-June 11

Re: Day of the year Program Question

Posted 12 June 2012 - 07:26 AM

View Postportman1, on 12 June 2012 - 03:36 PM, said:

The joption box asks them for the number


Yes, it does. But you don't do anything with the answer. Instead you read a number a second time (this time from the console) and then continue with that number.

And since apparently you never enter that second number when you test your program, it looks to you as if your program was hanging or doing nothing.
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Day of the year Program Question

Posted 12 June 2012 - 07:36 AM

As sepp2k mentionned, are you sure you provided intput to this statement ?

int input = keyboard.nextInt();

The user will enter a number on the JOptionPane but you do not process that number, instead you try to read another number from the console
Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7895
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

Re: Day of the year Program Question

Posted 12 June 2012 - 08:07 AM

How to debug:

Look where you want something to happen. In this case, it's


System.out.println(date);



That line isn't going to fail - if it does, you've got big, big problems. So if you get to that line, it's going to print whatever's in "date".

Now, you're in main, and you're not in a conditional branch, and there's no explicit exit from the program, so if the program terminates normally this line will be executed.

Does date have a value at that point? Yes: the if block preceding this line assigns a value to date in every branch, and includes an else branch, so it's impossible to get through that block without assigning a value to date.

Okay, so if the program gets to the end, you have to see the name of a month printed to standard output. From your report, you don't see this, therefore the program does not get to the end. Why?

Either it's exiting abnormally ("crashing") or it's blocked. You'd have said something if it were crashing, so I'm betting it's blocked. Is there a statement that blocks execution of the program? Yes, user input will do this.

Hypothesis: you're blocked at user input. Test this by replacing the input with a direct assignment:

   //  JOptionPane.showInputDialog("Enter the day of the year (1..365)");
        int input = 7;


And see what happens.

(yes, both pbl and sepp2k gave you the answer - but I want you to know how to find the answer)
Was This Post Helpful? 1
  • +
  • -

#7 portman1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-April 12

Re: Day of the year Program Question

Posted 12 June 2012 - 08:21 AM

I guess I'm not following, where else besides the input = keyboard.nextint am I asking for input
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7895
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

Re: Day of the year Program Question

Posted 12 June 2012 - 08:24 AM

JOptionPane.showInputDialog("Enter the day of the year (1..365)");



This is also a blocking input method.
Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2134
  • View blog
  • Posts: 3,274
  • Joined: 21-June 11

Re: Day of the year Program Question

Posted 12 June 2012 - 08:25 AM

View Postjon.kiparsky, on 12 June 2012 - 05:07 PM, said:

Does date have a value at that point?


Note that it's not actually necessary to ask yourself that question about local variables in Java. If there was a possible execution path in which date was used without having a value, the program would not compile.
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7895
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

Re: Day of the year Program Question

Posted 12 June 2012 - 08:30 AM

View Postsepp2k, on 12 June 2012 - 10:25 AM, said:

View Postjon.kiparsky, on 12 June 2012 - 05:07 PM, said:

Does date have a value at that point?


Note that it's not actually necessary to ask yourself that question about local variables in Java. If there was a possible execution path in which date was used without having a value, the program would not compile.



True. I should have said, "a visible value". That is a value that isn't "", which would be legal and look like nothing was printing. The point is, is it possible for this to execute without you seeing it? And the answer in this case is no, if you can see the console.
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2793
  • View blog
  • Posts: 11,827
  • Joined: 20-September 08

Re: Day of the year Program Question

Posted 12 June 2012 - 09:52 AM

You can actually make your job a lot easier by using java.util.Calendar and still stick to the brief
Was This Post Helpful? 0
  • +
  • -

#12 portman1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-April 12

Re: Day of the year Program Question

Posted 12 June 2012 - 11:41 AM

Okay so I figured out my printing issue..but now my boolean operands are wrong.

package program1;

import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class Program1 extends JFrame {

    private static final int LAST_DAY_OF_JANUARY = 31;
    private static final int LAST_DAY_OF_FEBRUARY = LAST_DAY_OF_JANUARY + 28;
    private static final int LAST_DAY_OF_MARCH = LAST_DAY_OF_FEBRUARY + 31;
    private static final int LAST_DAY_OF_APRIL = LAST_DAY_OF_MARCH + 30;
    private static final int LAST_DAY_OF_MAY = LAST_DAY_OF_APRIL + 31;
    private static final int LAST_DAY_OF_JUNE = LAST_DAY_OF_MAY + 30;
    private static final int LAST_DAY_OF_JULY = LAST_DAY_OF_JUNE + 31;
    private static final int LAST_DAY_OF_AUGUST = LAST_DAY_OF_JULY + 31;
    private static final int LAST_DAY_OF_SEPTEMBER = LAST_DAY_OF_AUGUST + 30;
    private static final int LAST_DAY_OF_OCTOBER = LAST_DAY_OF_SEPTEMBER + 31;
    private static final int LAST_DAY_OF_NOVEMBER = LAST_DAY_OF_OCTOBER + 30;
    private static final int LAST_DAY_OF_DECEMBER = LAST_DAY_OF_NOVEMBER + 31;

    public static void main(String[] args) {
        String num;
        int input;
        num = JOptionPane.showInputDialog("Enter the day of the year (1..365)");
        input = Integer.parseInt(num);
        String date;
        if (input <= LAST_DAY_OF_JANUARY) {
            date = "January";
        } else if (input > LAST_DAY_OF_JANUARY && input < LAST_DAY_OF_MARCH) {
            date = "February";
        } else if (input > LAST_DAY_OF_MARCH && input < LAST_DAY_OF_APRIL) {
            date = "March";
        } else if (input > LAST_DAY_OF_APRIL && input < LAST_DAY_OF_MAY) {
            date = "April";
        } else if (input > LAST_DAY_OF_MAY && input < LAST_DAY_OF_JUNE) {
            date = "May";
        } else if (input > LAST_DAY_OF_JUNE && input < LAST_DAY_OF_JULY) {
            date = "June";
        } else if (input > LAST_DAY_OF_JULY && input < LAST_DAY_OF_AUGUST) {
            date = "July";
        } else if (input > LAST_DAY_OF_AUGUST && input < LAST_DAY_OF_SEPTEMBER) {
            date = "August";
        } else if (input > LAST_DAY_OF_SEPTEMBER && input < LAST_DAY_OF_OCTOBER) {
            date = "September";
        } else if (input > LAST_DAY_OF_OCTOBER && input < LAST_DAY_OF_NOVEMBER) {
            date = "October";
        } else if (input > LAST_DAY_OF_NOVEMBER && input < LAST_DAY_OF_DECEMBER) {
            date = "November";
        } else  {
            date = "December";
        }
    
        System.out.println(date);
    }
}

Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2793
  • View blog
  • Posts: 11,827
  • Joined: 20-September 08

Re: Day of the year Program Question

Posted 12 June 2012 - 11:51 AM

You start going wrong from the second else..if (check the >)
Was This Post Helpful? 0
  • +
  • -

#14 portman1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-April 12

Re: Day of the year Program Question

Posted 12 June 2012 - 11:56 AM

View Postg00se, on 12 June 2012 - 11:51 AM, said:

You start going wrong from the second else..if (check the >)

I'm not following what your saying..All the greater thans are the same, why would the one be the issue?
Was This Post Helpful? 0
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2134
  • View blog
  • Posts: 3,274
  • Joined: 21-June 11

Re: Day of the year Program Question

Posted 12 June 2012 - 12:11 PM

View Postportman1, on 12 June 2012 - 08:56 PM, said:

View Postg00se, on 12 June 2012 - 11:51 AM, said:

You start going wrong from the second else..if (check the >)

I'm not following what your saying..All the greater thans are the same, why would the one be the issue?


Write down in English what the second else if does any why that is the correct way to determine whether you're in January (or as it turns out: why it isn't).

Spoiler

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2