Results 1 to 20 of 21
Thread: java newbie help
- 06-06-2011, 04:55 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
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
- 06-06-2011, 05:04 AM #2
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.
- 06-06-2011, 05:07 AM #3
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
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
- 06-06-2011, 05:14 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
whole code sorry about code tag i didnt knowJava 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 } }
- 06-06-2011, 05:20 AM #5
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.
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.Java Code:if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if (day < 1 || day > 31) {
- 06-06-2011, 05:28 AM #6
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
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.
- 06-06-2011, 05:31 AM #7
- 06-06-2011, 05:32 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
You need to check to see IF the day you are adding to the current day will change the month too, no?
- 06-06-2011, 05:39 AM #9
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
- 06-06-2011, 05:48 AM #10
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.
- 06-06-2011, 05:49 AM #11
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.
- 06-06-2011, 05:50 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
- 06-06-2011, 06:11 AM #13
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
- 06-06-2011, 06:15 AM #14
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.
- 06-06-2011, 06:25 AM #15
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
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.
- 06-06-2011, 06:40 AM #16
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
is this if statement correct?
Java Code:if (month == 4 || month == 6 || month == 9 || month == 11 && day > 30) { ++month; }
- 06-06-2011, 06:42 AM #17
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
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.
- 06-06-2011, 06:59 AM #18
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
- 06-06-2011, 07:07 AM #19
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 }
- 06-06-2011, 07:19 AM #20
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
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.
Similar Threads
-
(Newbie) Help with Java and XML
By sharktopus in forum AWT / SwingReplies: 0Last Post: 05-15-2011, 05:54 PM -
Newbie to Java.
By avinash87 in forum New To JavaReplies: 2Last Post: 04-30-2011, 08:36 AM -
Newbie iN java and need some help indeed!!
By davexc in forum Java AppletsReplies: 1Last Post: 07-13-2009, 05:34 PM -
'm a newbie of java
By ilysony in forum IntroductionsReplies: 7Last Post: 07-21-2008, 03:54 AM -
Help, java newbie
By baltimore in forum New To JavaReplies: 1Last Post: 08-07-2007, 12:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks