Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    dasu908 is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default java newbie help

    user input date in the format MM/DD/YYYY and a number of days a (between 1 and 20). Write a Java program to calculate the date A days after date. The new date must be in the format [Month DD, YYYY] and consider leap years.

    i can't figure out that when user input date 12/30/2011 and A is 5 result should be Jan 4 , 2012.

    i can't figure out how to do that pls help.

    thanks

    Scanner scan = new Scanner(System.in);
    System.out.print("Enter a Date"); // enter date
    String date = scan.next();

    System.out.print("Enter the Number of Days: "); // # of days
    int a = scan.nextInt();

    String m = date.substring(0, 2);
    String d = date.substring(3, 5);
    String y = date.substring(6, 10);

    int month = Integer.parseInt (m);
    int day = Integer.parseInt (d);
    int year = Integer.parseInt (y);


    String mName = null;

    switch (month) {

    case 1:
    mName = "January";
    break;
    case 2:
    mName ="February";
    break;
    ETC
    }

    // valid month and date
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
    if (day < 1 || day > 31) {
    System.out.print("False "); }
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
    if (day < 1 || day > 30) {
    System.out.print("False "); }
    } else if (year % 400==0 || year % 4==0 && year % 100 != 0) {
    if (day < 1 || day > 29) {
    System.out.print("False "); }
    } else if (day < 1 || day > 28) {
    System.out.print("False ");
    }

    // Check if a is valid between 1 and 20.
    if (a < 1 || a > 20) {
    System.out.println("Invalid a value");
    } else
    day = (day + a);


    System.out.println("The Date is: " +mName + " " + day + ", " + year); // ComputeResult

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Are you allowed to use the Date or Calendar classes? If so they will make your task a lot easier. If not you need a check to see if adding X days will roll past the end of the year and if it does, increment your year.

  3. #3
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    im confused as to where youve actually done the calculations in order to figure out the new date
    it seems that you've copied and pasted only snippits of your code, can you please paste the entire thing?
    and next time enclose it in code tags

  4. #4
    dasu908 is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    
    public class date {
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		System.out.print("Enter a Date"); // enter date
    		String date = scan.next();
    
    		System.out.print("Enter the Number of Days: "); // # of days
    		int a = scan.nextInt();
    
    		String m = date.substring(0, 2);
    		String d = date.substring(3, 5);
    		String y = date.substring(6, 10);
    
    		int month = Integer.parseInt (m);
    		int day = Integer.parseInt (d);
    		int year = Integer.parseInt (y);
    
    
    		String mName = null;
    
    		switch (month) {
    
    		case 1:
    			mName = "January";
    		break;
    		case 2:
    			mName ="February";
    		break;
    		case 3:
    			mName ="March";
    			break;
    		case 4:
    			mName ="April";
    			break;
    		case 5:
    			mName="May";
    			break;
    		case 6:
    			mName="June";
    			break;
    		case 7:
    			mName="July";
    			break;
    		case 8:
    			mName="August";
    			break;
    		case 9:
    			mName="September";
    			break;
    		case 10:
    			mName="October";
    			break;
    		case 11:
    			mName="November";
    			break;
    		case 12:
    			mName="December";
    			break;
    		default:
    			mName="Invalid Month";
    		}
    
    		// valid month and date
    		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
    		if (day < 1 || day > 31) {
    		System.out.print("False "); }
    		} else if (month == 4 || month == 6 || month == 9 || month == 11) {
    		if (day < 1 || day > 30) {
    		System.out.print("False "); }
    		} else if (year % 400==0 || year % 4==0 && year % 100 != 0) {
    		if (day < 1 || day > 29) {
    		System.out.print("False "); }
    		} else if (day < 1 || day > 28) {
    		System.out.print("False ");
    		}
    
    		// Check if a is valid between 1 and 20.
    		if (a < 1 || a > 20) {
    		System.out.println("Invalid a value");
    		} else
    		day = (day + a);
    
    
    		System.out.println("The Date is: " + mName + " " + day + ", " + year); // ComputeResult 
    	}
    
    }
    whole code sorry about code tag i didnt know

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    OK so all you have done is add 5 to day. Where is you error checking? Like I said in my first reply, you need to make sure adding 5 does not go over the total number of days allowed for that month.

    Java Code:
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
        if (day < 1 || day > 31) {
    By the way, that first if statement is pointless. If the value of day is less than 1 or greater than 31 then it is invalid regardless of what month it is.

  6. #6
    dasu908 is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    yes i know that it should not go over total number of days and jump to next month and if its like December 30, 2011 then it should change the year too but i have tried everything i know and it don't seem to be working.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by dasu908 View Post
    i have tried everything i know
    Really? All I have seen is day = day + a. That hardly seems like everything. In my previous posts there is a word that was used predominantly: if. What does that suggest to you?

  8. #8
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    You need to check to see IF the day you are adding to the current day will change the month too, no?

  9. #9
    dasu908 is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by airowe View Post
    You need to check to see IF the day you are adding to the current day will change the month too, no?
    yes if its may 31 and A is 1 then month should change to june

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Exactly so go do it.

    I have one suggestion, you might find it easier if you have another variable that holds the max number of days allowed for that month. The you can check IF day + a is greater than that value.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    If you do as I suggest then you can replace that large if else if statement checking valid day and month with a single if statement.

  12. #12
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by dasu908 View Post
    yes if its may 31 and A is 1 then month should change to june
    Right, but it doesn't. It outputs May 36 instead.

  13. #13
    dasu908 is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by airowe View Post
    Right, but it doesn't. It outputs May 36 instead.
    yaa that's what i am trying to figure out

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    It doesn't seem that way. It seems that you are just waiting for someone to spoonfeed you the code. We have given you plenty of hints how to do this. Now try writing some code. If you get stuck post your code and ask a specific question. Copy and paste the exact error message as well if you get any.

  15. #15
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Once you figure out the month, then you need an if statement to check if the days added to the current day will cause a new month to be stored.
    If you use Junky's idea, you will have a variable that represents the max number of days in the month of May.
    Then, you'll need to test whether day + a > maxMayDays. IF it does, change the month to June. Same thing for the rest of the months, but you'll need to make a special condition for the month of December.

  16. #16
    dasu908 is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    is this if statement correct?

    Java Code:
    if (month == 4 || month == 6 || month == 9 || month == 11 && day > 30) {
    			++month;
    		}

  17. #17
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    You don't want to test for day to be over the max. You want to test for day + a. Testing for day will lead you to accepting invalid user input.

    ETA: That is just based upon your current coding.

    Take a piece of paper and write out what you want to do in plain English. Then, translate it into code.

    Something like:

    1) Tell user to input a date in this format "MM/DD/YYYY"
    2) While input is not valid, tell user and ask for input again.
    3) If date input is valid, store values in the appropriate variables.
    4) Tell user to input number of days to advance.
    5) While input is not valid, tell user and ask for input again.
    6) Add days to advance to date input.
    7) If new day is past month's day threshold, advance month.
    8) If new month is past year's month threshold, advance year.
    9) Output new date.
    Last edited by airowe; 06-06-2011 at 06:50 AM.

  18. #18
    dasu908 is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by airowe View Post
    You don't want to test for day to be over the max. You want to test for day + a. Testing for day will lead you to accepting invalid user input.
    like u suggested i added (day + a) > 30 but month still don't change.

    Java Code:
    if (month == 4 || month == 6 || month == 9 || month == 11 && (day + a) > 30) {
    			++month;
    		}

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Make sure you conditions have to correct precedence. You might want to put brackets around all the or's.

    If you had followed my advice about using the max number of days variable it would be a simple if.
    Java Code:
    if((days + a) > maxDays) {
        inc month
    }

  20. #20
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    If I were writing your code, I'd check for validation right after you receive input and then go into your switch statements.

    Using Junky's method, declare a maxDays variable with your other variables. Then, depending on which month it is, set that variable to it's appropriate value. He's given you the function you need to complete the rest.

Page 1 of 2 12 LastLast

Similar Threads

  1. (Newbie) Help with Java and XML
    By sharktopus in forum AWT / Swing
    Replies: 0
    Last Post: 05-15-2011, 05:54 PM
  2. Newbie to Java.
    By avinash87 in forum New To Java
    Replies: 2
    Last Post: 04-30-2011, 08:36 AM
  3. Newbie iN java and need some help indeed!!
    By davexc in forum Java Applets
    Replies: 1
    Last Post: 07-13-2009, 05:34 PM
  4. 'm a newbie of java
    By ilysony in forum Introductions
    Replies: 7
    Last Post: 07-21-2008, 03:54 AM
  5. Help, java newbie
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 12:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •