Help Please

I am new to Java and stuck on a program assignment. I know it is probably something silly, but I can't find the problem. We have to create a proogram using Gregorian Calendar that starts January 1, 2007 and runs all the way through December 31, 2007. All I can figure is that it has to be in my top loop, but I'm not sure how to fix it.Below is the program. Any help would be greatly appreciated.

import java.util.*;

public class MonthlyCalendar4 {

public static void main(String[] args) {

GregorianCalendar calendar = new GregorianCalendar(2007, Calendar.JANUARY, 1);

int month = calendar.get(Calendar.MONTH);

int year = calendar.get(Calendar.YEAR);

String monthName = " ";

int weekday = calendar.get(Calendar.DAY_OF_WEEK);

for (int x = 0; x < 12; x++) {

do {

calendar.add(Calendar.DAY_OF_MONTH, 1);

weekday = calendar.get(Calendar.DAY_OF_WEEK);

} while (calendar.get(Calendar.MONTH) == month);

switch (month) {

case 0:

monthName = "January";

break;

case 1:

monthName = "February";

break;

case 2:

monthName = "March";

break;

case 3:

monthName = "April";

break;

case 4:

monthName = "May";

break;

case 5:

monthName = "June";

break;

case 6:

monthName = "July";

break;

case 7:

monthName = "August";

break;

case 8:

monthName = "September";

break;

case 9:

monthName = "October";

break;

case 10:

monthName = "November";

break;

case 11:

monthName = "December";

break;

}

System.out.println(monthName + " " + year);

System.out.println();

System.out.println("Sun Mon Tue Wed Tru Fri Sat");

for (int i = Calendar.SUNDAY; i < weekday; i++) {

System.out.print("");

}

do {

int day = calendar.get(Calendar.DAY_OF_MONTH);

if (day < 10) {

System.out.print(" ");

}

System.out.print(" " + day + " ");

if (weekday == Calendar.SATURDAY) {

System.out.println();

}

calendar.add(Calendar.DAY_OF_MONTH, 1);

weekday = calendar.get(Calendar.DAY_OF_WEEK);

} while (calendar.get(Calendar.MONTH) == month);

}

}

}

[2332 byte] By [Neumanna] at [2007-11-27 10:19:06]
# 1

welcome to this forum. I haven't looked at your code yet and will explain why.

When you post here, there are a couple of "rules" that you should follow if you want to get helpful answers to your questions. These include:

1. Use a Subject Heading that briefly describes your problem. "Help Please" doesn't.

2.When posting code, please use code tags. Just highlight your code and click the code button is all you have to do. It makes your code currently unreadable code readable.

3. "My code doesn't work" doesn't tell us enough. How doesn't it work? Are you receiving error messages? If so, can you post the messages? etc...

4. Be as precise as possible when describing what your code is supposed to do. I can't really tell what you want from what you posted above.

5. Remember we are all volunteers. If you are patient and considerate and show that you are making an effort, you will probably get help.

6. Have a thick skin. Things can sometimes get a little rough here, but we mean well and generally post information that is in your best interest.

7. If someone puts in some effort on your problem, show them your appreciation. That's how we're "paid" here.

Good luck and happy coding.

Message was edited by:

petes1234

petes1234a at 2007-7-28 16:55:38 > top of Java-index,Java Essentials,New To Java...
# 2

1. Use a better topic, you wouldn't be here if you didn't need help.

2. When posting code, use code tags to keep it formatted. Paste code (from source not first post), highlight it and click code button. Please repost code.

floundera at 2007-7-28 16:55:38 > top of Java-index,Java Essentials,New To Java...
# 3

Thank you for your tips. The program compiles and runs without any errors, but my output is not showing correctly.A little background to this project, there are four phases to it. Phase 1, we had to create a new Gregorian Calendar object and use a for loop, do loop and if statement to pull in the calendar for the month of July. Phase 2 was creating a switch statement to print the heading of month and year. Phase 3 was adding a scanner object to allow the month to be input and Phase 4 was to create a loop statement that will generate a calendar for Jan through Dec of the current year. When I run the program, it displays each months and the first day of each month, but not the rest of the days of the month. I've reattached the code below and would it help to send the code for phase 2 to show how it is suppose to output?

Thank you for your patience.

import java.util.*;

public class MonthlyCalendar4 {

public static void main(String[] args) {

GregorianCalendar calendar = new GregorianCalendar(2007, Calendar.JANUARY, 1);

int month = calendar.get(Calendar.MONTH);

int year = calendar.get(Calendar.YEAR);

String monthName = " ";

int weekday = calendar.get(Calendar.DAY_OF_WEEK);

for (int x = 0; x < 12; x++) {

do {

calendar.add(Calendar.DAY_OF_MONTH, 1);

weekday = calendar.get(Calendar.DAY_OF_WEEK);

} while (calendar.get(Calendar.MONTH) == month);

switch (month) {

case 0:

monthName = "January";

break;

case 1:

monthName = "February";

break;

case 2:

monthName = "March";

break;

case 3:

monthName = "April";

break;

case 4:

monthName = "May";

break;

case 5:

monthName = "June";

break;

case 6:

monthName = "July";

break;

case 7:

monthName = "August";

break;

case 8:

monthName = "September";

break;

case 9:

monthName = "October";

break;

case 10:

monthName = "November";

break;

case 11:

monthName = "December";

break;

}

System.out.println(monthName + " " + year);

System.out.println();

System.out.println("Sun Mon Tue Wed Tru Fri Sat");

for (int i = Calendar.SUNDAY; i < weekday; i++) {

System.out.print("");

}

do {

int day = calendar.get(Calendar.DAY_OF_MONTH);

if (day < 10) {

System.out.print(" ");

}

System.out.print(" " + day + " ");

if (weekday == Calendar.SATURDAY) {

System.out.println();

}

calendar.add(Calendar.DAY_OF_MONTH, 1);

weekday = calendar.get(Calendar.DAY_OF_WEEK);

} while (calendar.get(Calendar.MONTH) == month);

}

}

}

Message was edited by:

Neumann

Neumanna at 2007-7-28 16:55:38 > top of Java-index,Java Essentials,New To Java...
# 4

Can you post the actual output of your code and what you are expecting to get.

floundera at 2007-7-28 16:55:38 > top of Java-index,Java Essentials,New To Java...
# 5

Sure, but the dates are not lining up under the correct hheading when I post this message. Below is the code that I used for phase 2 of the project and I got the following output:

-jGRASP exec: java MonthlyCalendar2

July 2007

Sun Mon Tue Wed Tru Fri Sat

1234567

891011121314

15161718192021

22232425262728

293031

-jGRASP: operation complete.

Here is the code I used for it:

import java.util.*;

public class MonthlyCalendar2 {

public static void main(String[] args) {

GregorianCalendar calendar = new GregorianCalendar();

int month = calendar.get(Calendar.MONTH);

calendar.set(Calendar.DAY_OF_MONTH, 1);

int weekday = calendar.get(Calendar.DAY_OF_WEEK);

int year = calendar.get(Calendar.YEAR);

String monthName = " ";

switch (month) {

case 0:

monthName = "January";

break;

case 1:

monthName = "February";

break;

case 2:

monthName = "March";

break;

case 3:

monthName = "April";

break;

case 4:

monthName = "May";

break;

case 5:

monthName = "June";

break;

case 6:

monthName = "July";

break;

case 7:

monthName = "August";

break;

case 8:

monthName = "September";

break;

case 9:

monthName = "October";

break;

case 10:

monthName = "November";

break;

case 11:

monthName = "December";

break;

}

System.out.println("" + monthName + " " + year);

System.out.println();

System.out.println("Sun Mon Tue Wed Tru Fri Sat");

for (int i = Calendar.SUNDAY; i < weekday; i++) {

System.out.print("");

}

do {

int day = calendar.get(Calendar.DAY_OF_MONTH);

if (day < 10) {

System.out.print(" ");

}

System.out.print(" " + day + " ");

if (weekday == Calendar.SATURDAY) {

System.out.println();

}

calendar.add(Calendar.DAY_OF_MONTH, 1);

weekday = calendar.get(Calendar.DAY_OF_WEEK);

} while (calendar.get(Calendar.MONTH)==month);

}

}

Message was edited by:

Neumann

Neumanna at 2007-7-28 16:55:38 > top of Java-index,Java Essentials,New To Java...
# 6

Sorry, I didn't add the output I was getting when I run the program in question.

-jGRASP exec: java MonthlyCalendar4

January 2007

Sun Mon Tue Wed Tru Fri Sat

1 January 2007

Sun Mon Tue Wed Tru Fri Sat

3

January 2007

Sun Mon Tue Wed Tru Fri Sat

5 January 2007

Sun Mon Tue Wed Tru Fri Sat

7 January 2007

Sun Mon Tue Wed Tru Fri Sat

9 January 2007

Sun Mon Tue Wed Tru Fri Sat

11 January 2007

Sun Mon Tue Wed Tru Fri Sat

13 January 2007

Sun Mon Tue Wed Tru Fri Sat

15 January 2007

Sun Mon Tue Wed Tru Fri Sat

17

January 2007

Sun Mon Tue Wed Tru Fri Sat

19 January 2007

Sun Mon Tue Wed Tru Fri Sat

21 January 2007

Sun Mon Tue Wed Tru Fri Sat

23

-jGRASP: operation complete.

Neumanna at 2007-7-28 16:55:38 > top of Java-index,Java Essentials,New To Java...
# 7

Yes, phase 2 would be nice. But then again, if you have the phase 2 code working correctly, why aren't you using it here?

You have many logic errors in your code as if you threw it together somewhat randomly. I recommend that you break down your problem into small bits and code each bit correctly before moving to the next part.

Addendum: I see that you've now posted it. One reason you can't just extract phase 2 and use it is that you put everything into the main method. That is a bad thing to do, if only for this very reason. Learn about creating methods and use them. Also a little OOP wouldn't hurt.

Addendum 2: So your program only prints out *12* dates. Hmmmmmmmmmm......

Message was edited by:

petes1234

Message was edited by:

petes1234

petes1234a at 2007-7-28 16:55:38 > top of Java-index,Java Essentials,New To Java...
# 8

So MonthlyCalendar2 works. You've got it easy then. What you need to do is this:

1) Change MonthlyCalender2 so that it is its own indepedent method.

2) Test this method out on several months to see if it accurately represents these months.

3) Write a separate routine that loops 12 times and calls your monthlyCal2 method above on each go through the loop.

and you're done. Why make it hard on yourself?

petes1234a at 2007-7-28 16:55:38 > top of Java-index,Java Essentials,New To Java...
# 9

Oh, I would love to make it easier; however, we have not reached that point in class yet. We've only covered predefined methods.

Neumanna at 2007-7-28 16:55:38 > top of Java-index,Java Essentials,New To Java...
# 10

> Oh, I would love to make it easier; however, we have

> not reached that point in class yet. We've only

> covered predefined methods.

Boy, that really sucks. So you are telling me that you are supposed to make one great big huge main method and shove all this logic into that method? That's both ugly and a bad practice to get into. It makes for some debugging nightmares and for lack of reusability of code.

If I were to do something like this, I'd make a class called MyMonth that you feed the month number and year into its constructor and it figures out the first weekday, the number of days of the month and the month name. Then I'd have an instance display method that would let MyMonth objects display themselves. But if you can't even have stand-alone methods, then for now ignore all of this.

petes1234a at 2007-7-28 16:55:38 > top of Java-index,Java Essentials,New To Java...
# 11

Yeah, it does really suck because everything starts looking like garbage. Talk about pulling your hair out. Thanks again for your help. I've tried everything I could think of with no luck.

Neumanna at 2007-7-28 16:55:38 > top of Java-index,Java Essentials,New To Java...
# 12

don't give up. You can still use your calendar2 program as a nucleus of the loop. just put a loop around it which is called 12 times. On each run through the loop, you update the current month, then go through the calendar2 code with the updated month, and that should just about do it.

petes1234a at 2007-7-28 16:55:38 > top of Java-index,Java Essentials,New To Java...
# 13

OK, thank you so much. That took care of it. The first thing I need to learn out of this class is to stop over thinking everything. That would help me quite a bit. Again, you guys/gals are wonderful for offering up your time for us beginners.

Neumanna at 2007-7-28 16:55:38 > top of Java-index,Java Essentials,New To Java...