need help with this program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: need help with this program

  1. #1
    Join Date
    Oct 2008
    Posts
    5

    Exclamation 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.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    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.

  3. #3
    Join Date
    Oct 2008
    Posts
    5

    Exclamation 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

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    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'.

  5. #5
    Join Date
    Oct 2008
    Posts
    5

    Exclamation 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);

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    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.

  7. #7
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,442

    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

  8. #8
    Join Date
    Oct 2008
    Posts
    5

    Exclamation 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.

  9. #9
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    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 &#91;code] above your code and &#91;/code] below your code.
    2) Don't post code without stating what is wrong with it.

    Good luck.

  10. #10
    Join Date
    Oct 2008
    Posts
    5

    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);
    }
    }

  11. #11
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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 &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  12. #12
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center