12 Replies - 530 Views - Last Post: 01 July 2011 - 11:04 PM Rate Topic: -----

#1 Jay_91  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 20-June 11

Date Converter Program problem

Posted 01 July 2011 - 08:33 PM

So I am trying to create this code that, when given a number between 1-366, I get the corresponding month and day in the form of "month/day". The code is below:

import java.io.*;

public class DateConverter {
  // Example:
  //	java DateConverter 365
  // should print
  //	 12/30
  public static void main (String [ ] args) {
    int dayOfYear = 0;
    try {
      dayOfYear = Integer.parseInt (args[0]);
    } catch (NumberFormatException e) {
      e.printStackTrace();
    }
    int month, dateInMonth, temp, daysInMonth;
    month = 1;
    daysInMonth = 31;
    while (dayOfYear > daysInMonth) {
    	temp = dayOfYear - daysInMonth; 

      if (month == 2) {
	daysInMonth = 29;
      } else if (month == 4 || month == 6 || month == 9 || month == 11) {
	daysInMonth = 30;
      } else {
	daysInMonth = 31;
      }
      month++;
      
    }
    dateInMonth = dayOfYear;
    System.out.println (month + "/" + dateInMonth);
  }
}




However, on line 1, it says that import will never be used, and on line 15, it says that the local variable temp is never read. So I was wondering where I could place temp so that it wouldn't give me this error. I wanted to create a temp variable so that every time I subtracted daysInMonth from dayOfYear, I could store that value and use the new value when it loops. Thanks for any help =D

Is This A Good Question/Topic? 0
  • +

Replies To: Date Converter Program problem

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Date Converter Program problem

Posted 01 July 2011 - 08:42 PM

First is because import java.io.*; is not used in your project and temp is not used. You didnt use it any where after giving it value. Where did you plan to use it?
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Date Converter Program problem

Posted 01 July 2011 - 08:52 PM

I guess everything has been said :)
You don't use any class from java.io, you do not use the value stored in the variable temp so why assigning a value to it ?
Was This Post Helpful? 1
  • +
  • -

#4 Jay_91  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 20-June 11

Re: Date Converter Program problem

Posted 01 July 2011 - 08:54 PM

I thought I had assigned it a value when I did temp = dayOfYear - daysInMonth?
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Date Converter Program problem

Posted 01 July 2011 - 09:00 PM

Yes you do assign value to it, but you didnt use its value any where, did you?
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Date Converter Program problem

Posted 01 July 2011 - 09:00 PM

View PostJay_91, on 01 July 2011 - 11:54 PM, said:

I thought I had assigned it a value when I did temp = dayOfYear - daysInMonth?

Yes but you do not use temp later on so why assigning a value to it ?
You just waste CPU cycles
Was This Post Helpful? 1
  • +
  • -

#7 Jay_91  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 20-June 11

Re: Date Converter Program problem

Posted 01 July 2011 - 09:04 PM

Oh I get it now! Thanks guys xD
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Date Converter Program problem

Posted 01 July 2011 - 09:06 PM

:)
Was This Post Helpful? 0
  • +
  • -

#9 Jay_91  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 20-June 11

Re: Date Converter Program problem

Posted 01 July 2011 - 09:38 PM

Sorry I have another quesion now... how would I set the daysInMonth according to the month? Because now when I do 61 days, I should be getting 3/1, but instead I get 2/30, so I'm thinking it's probably because my daysInMonth have not changed and the program is counting daysInMonth as if it was still 31.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Date Converter Program problem

Posted 01 July 2011 - 09:42 PM

View PostJay_91, on 02 July 2011 - 12:38 AM, said:

Sorry I have another quesion now... how would I set the daysInMonth according to the month? Because now when I do 61 days, I should be getting 3/1, but instead I get 2/30, so I'm thinking it's probably because my daysInMonth have not changed and the program is counting daysInMonth as if it was still 31.

do not know of a month of 61 days, the max one I know has 31 days at least in our actual mostly used Gregorian Calendar
So what s your question ?
Was This Post Helpful? 0
  • +
  • -

#11 Jay_91  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 20-June 11

Re: Date Converter Program problem

Posted 01 July 2011 - 10:04 PM

The program is supposed to change the number of days into a "month/day" format, so if put in 61 as my argument, I should be getting back 3/1 since 61 days corresponds to March 1 (31 days in January + 29 days in February + 1 day in March = 61 days altogether). Right now I am getting 2/30 for 61 days, but I should be getting 3/1 instead (for March 1). I'm thinking this is because I haven't changed daysInMonth yet, so it is counting February as a 31-day month. How could I fix this?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Date Converter Program problem

Posted 01 July 2011 - 10:13 PM

better to


int daysInMonth(int year, int month) (
    int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if(isLeapYear(year))
       daysInMonth[1] = 29;
    return daysInMonth[month]:


Now, your turn to write the isLeapYear() method :)
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,641
  • Joined: 20-September 08

Re: Date Converter Program problem

Posted 01 July 2011 - 11:04 PM

You can do


	Calendar cal = Calendar.getInstance();
	cal.set(Calendar.DAY_OF_YEAR, Integer.parseInt(args[0]));
	System.out.printf("%d/%d\n", 1+cal.get(Calendar.MONTH), cal.get(Calendar.DATE));


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1