Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: i think something wrong with array...plz help!!

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default i think something wrong with array...plz help!!

    hey...i wrote this code to check the validity of a date
    the errors are at line 8 and mentioned as comment with it!!
    import java.io.*;
    class Date//checks validity of a date
    {
    	int days []=new int [12];
    	int d,m,y;
    	Date ( )
    	{
    		days []={31,28,31,30,31,30,31,31,30,31,30,31};// <-- 'not a staement' and '; expected' error
    	}
    	void accept( )throws Exception
    	{
    		BufferedReader obl=new BufferedReader (new InputStreamReader(System.in));
    		d=Integer.parseInt(obl.readLine());
    		m=Integer.parseInt(obl.readLine());
    		y=Integer.parseInt(obl.readLine());
    	}
    	boolean isLeap()
    	{
    		if (y%4==0)
    		return true;
    		else
    		return false;
    	}
    	boolean checkDate( )
    	{
    		if (isLeap == true)
    		days[1]++;
    		if(m<=12)
    		{
    			if(d<=days[m-1])
    			{
    				return true;
    			}
    		}
    		else
    		return false;
    	}
    	public static void main(String args [])throws Exception
    	{
    		Date d1=new Date();
    		d1.accept();
    		d1.isLeap();
    		if(d1.checkDate()==true)
    			System.out.println("Date is valid");
    	}
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: i think something wrong with array...plz help!!

    import java.io.*;
     
    public class Date//checks validity of a date
    {
     
        int d, m, y;
        public Date() {
            int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};// Short syntax array - size == amount between { and }
        }
     
        /*Or you can use the following syntax:
         * int[] days = new int[sizeHere];
         * days[0] = 1;
         * days[1] = 2;
         * days[2] = 3;
         * days[3] = 4;
         * days[4] = 5;
         * days[5] = 6;
         */
        public void accept() throws Exception {
            BufferedReader obl = new BufferedReader(new InputStreamReader(System.in));
            d = Integer.parseInt(obl.readLine());
            m = Integer.parseInt(obl.readLine());
            y = Integer.parseInt(obl.readLine());
        }
     
        public boolean isLeap() {
            if (y % 4 == 0) {
                return true;
            } else {
                return false;
            }
        }
     
        public boolean checkDate() {
            if (isLeap == true) {
                days[1]++;
            }
            if (m <= 12) {
                if (d <= days[m - 1]) {
                    return true;
                }
            } else {
                return false;
            }
        }
     
        public static void main(String args[]) throws Exception {
            Date d1 = new Date();
            d1.accept();
            d1.isLeap();
            if (d1.checkDate() == true) {
                System.out.println("Date is valid");
            }
        }
    }

    There you go, anything you don't understand, just ask.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    aks.1393 (January 14th, 2011)

  4. #3
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: i think something wrong with array...plz help!!

    Quote Originally Posted by newbie View Post
    There you go, anything you don't understand, just ask.
    I have some things I don't understand

    1. Why did you post incorrect code?

    2. Why did you define a variable in the constructor, that is never read locally, which generates a warning?

    3. Why did you refer to days in checkDate() when it is only defined in the scope of the Date constructor?

    4. Why did you leave the () off of isLeap in the checkDate() method. Don't you want to call the isLeap method? If not, why didn't you define a boolean field isLeap as a member of the Date class?

  5. The Following User Says Thank You to DavidFongs For This Useful Post:

    aks.1393 (January 14th, 2011)

  6. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: i think something wrong with array...plz help!!

    public boolean isLeap(int year) {
            if (year % 4 == 0) {
                return true;
            } else {
                return false;
            }
        }

    Not sure why you're doing a BufferReader which used an InputStreamReader which uses System.in;

    Why not just use a Scanner

    Scanner console = new Scanner(System.in);

    Also, change your checkDate method to checkDate(int day, month, int year)

    A Date has a day, a month, and a year.

    public Date(int day, int month, int year)

    If year is leap year, accept February dates from 1 to 29.

    Otherwise accept February dates only from 1 to 28.

    Accept dates as valid for the other months as long as they are within the appropriate ranges.

    Don't accept months less than 1 or greater than 12.

    Don't know how you want to do the year thing.

  7. The Following User Says Thank You to javapenguin For This Useful Post:

    aks.1393 (January 14th, 2011)

  8. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: i think something wrong with array...plz help!!

    thnx...it was giving errors but i worked it out later!!

  9. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: i think something wrong with array...plz help!!

    n thnx javapenguin n david fongs....u cleared my concepts!!

  10. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: i think something wrong with array...plz help!!

    Quote Originally Posted by DavidFongs View Post
    I have some things I don't understand

    1. Why did you post incorrect code?

    2. Why did you define a variable in the constructor, that is never read locally, which generates a warning?

    3. Why did you refer to days in checkDate() when it is only defined in the scope of the Date constructor?

    4. Why did you leave the () off of isLeap in the checkDate() method. Don't you want to call the isLeap method? If not, why didn't you define a boolean field isLeap as a member of the Date class?
    Possibly because he asked for correction of his Array? Which is what I believe I did.
    Sorry If I didn't read his entire program... but I let him keep his original structure because I don't know what his specifications are.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Something wrong with array
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2010, 07:34 AM
  2. WHAT IS WRONG HERE PLEASE?
    By mjava in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 27th, 2010, 08:29 PM
  3. What's wrong?!
    By deeerek in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 22nd, 2010, 07:11 PM
  4. don't know what's wrong
    By james in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 15th, 2010, 07:37 PM
  5. Where am I going wrong? :)
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 18th, 2009, 12:03 AM