-
October 2nd, 2008, 10:17 AM
#1
need help with this program
Create a program that prompts the user to enter the month and year. The program will then display the number of days in the month. For example, if the user enters month 8 and year 2007, the program will display "August 2007 has 31 days". Use Scanner for input and System.out.println or printf for output. Also leap year must be part of your calculation.
-
October 2nd, 2008, 10:27 AM
#2
Re: need help with this program
If you are hoping someone is going to write your homework for you then you are sadly mistaken. And if you can't even be bothered to write anything other than just the question then the only advice you're going to get is don't be so **** lazy.
-
October 2nd, 2008, 12:53 PM
#3
Re: need help with this program
I'm sorryI wasn't trying to get someone to do my work for me. I was just trying to see if i could get some asstance it would be greatly appreated. This is what if have so far. Not sure if I'm on th right track or not.
import javax.swing.*;
public class DaysInAMonth {
public static void mian(String args[]) {
// Prompt the user to enter a month and year
String yearString = JOptionPane.showInputDialog("Enter a month " + year + "?");
// Convert the string into a int value
int month = Integer.parseInt(monthString);
// Convert the string into a int value
int year = Integer.paerseInt(yearString);
// Check if year is a leap year
boolean isLeapYear =
(year % 4 == 0 && year % 100 != 0) || (year % 400 ==0);
// Display the results in a message dialog box
-
October 2nd, 2008, 02:48 PM
#4
Re: need help with this program
The question says "use Scanner for input", you are using a JOptionPane so you have failed before you have even started.
As for being on the right track, the little code you have shown is heading in the right general direction although it does have mistakes (apart from not the above) - ie what is the variable 'monthString'.
-
October 2nd, 2008, 03:24 PM
#5
Re: need help with this program
So is this in the right direction?
import java.util.scanner;
public class DaysInAMonth {
public static void mian(String args[]) {
// Create a Scanner
Scanner scanner = new Scanner(System.in);
-
October 2nd, 2008, 04:41 PM
#6
Re: need help with this program
So is this in the right direction?
Look, I'm willing to help if you are stuck but I'm not going to give help on a line by line basis. Read the question and decide for yourself if it's the right direction.
When you have completed a reasonable amount of the program and have a sensible question/real problem post it and I/someone will help.
-
October 2nd, 2008, 05:00 PM
#7
Re: need help with this program
Do your programming in small steps. Use the Scanner class to read in some data and print it out. Then move on to the next part.
Norm
-
October 2nd, 2008, 09:08 PM
#8
Re: need help with this program
Look thanks for the the supoort but programming takes time. Check this out but, im trying to implement promting the user to enter a month aslo if anyone could help would be greatly appreacted. And I haven't been waiting for someone to do it for me this is difficult for me maybe not for you thanks agian for your help though.
import java.util.*;
import java.io.*;
public class GettingDaysInMonth {
public static void main(String[] args) throws IOException{
// Prompt the user to enter a month and a year
int year = 1;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter year : ");
try{
// Check year for a leap year
year = Integer.parseInt(in.readLine());
if (year < 1900 || year > 2100){
System.out.println("Please enter year greater than 1900 and less than 2100.");
System.exit(0);
}
}
catch(NumberFormatException ne){
System.out.print(ne.getMessage() + " is not a valid entry.");
System.out.println("Please enter a four digit number.");
System.exit(0);
}
// Display the results in a message dialog box
Calendar cal = new GregorianCalendar(year, Calendar.FEBRUARY, 1);
int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.print("Number of days : " + days);
}
}
Last edited by XxXxJTxXxX; October 2nd, 2008 at 09:29 PM.
-
October 2nd, 2008, 09:18 PM
#9
Re: need help with this program
Another bit of advice:
1) When posting code, use code tags so that your code is readable. To do this, place [code] above your code and [/code] below your code.
2) Don't post code without stating what is wrong with it.
Good luck.
-
October 2nd, 2008, 09:41 PM
#10
Re: need help with this program
Need help implementing the user to enter a month also.
import java.util.*;
import java.io.*;
public class GettingDaysInMonth {
public static void main(String[] args) throws IOException{
// Create a Scanner
Scanner scanner = new Scanner(System.in);
// Convert the string into an int value
int year = 1;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter year : ");
try{
year = Integer.parseInt(in.readLine());
// Check if the year is a leap year
if (year < 1900 || year > 2100){
System.out.println("Please enter year greater than 1900 and less than 2100.");
System.exit(0);
}
}
catch(NumberFormatException ne){
System.out.print(ne.getMessage() + " is not a valid entry.");
System.out.println("Please enter a four digit number.");
System.exit(0);
}
// Display the results in a message dialog box
Calendar cal = new GregorianCalendar(year, Calendar.FEBRUARY, 1);
int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.print("Number of days : " + days);
}
}
-
October 3rd, 2008, 04:28 AM
#11
Re: need help with this program
You don't seem to be reading the replies to your posts - when posting code, put it in the CODE tags so that it stays formatted.
When asking questions be clear and specific - "I want help with..." isn't going to cut it.
You may find How To Ask Questions The Smart Way helpful.
Questions are the important thing, answers are less important. Learning to ask a good question is the heart of intelligence. Learning the answer---well, answers are for students. Questions are for thinkers...
R. Schank
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
October 3rd, 2008, 05:36 AM
#12
Re: need help with this program
Need help implementing the user to enter a month also.
Ask yourself: What is the difference between getting the user to enter a month and getting the user to enter a year (which you have already coded)?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|