Results 1 to 6 of 6
  1. #1
    lk1001 is offline Member
    Join Date
    Feb 2010
    Posts
    8
    Rep Power
    0

    Default Compile Errors and need to add a second static

    Hey all,

    I am having some compile errors and I was wondering if anyone could point me in the right direction here.

    Here is my code

    import java.util.Scanner;



    public class Lab
    {

    public static void main (String [] args)
    {

    public static final boolean isLeapYear ( short yyyy )
    {

    if ( yyyy % 4 != 0 ) return false;
    if ( yyyy % 100 != 0 ) return true;
    if ( yyyy % 400 != 0 ) return false;
    if ( yyyy % 3200 != 0 ) return true;
    return false;
    }


    while (decision ==0)
    {
    System.out.print("Please enter the desired year - ");
    yyyy= input.nextShort();;

    System.out.print("\n "The number of days in + (yyyy) + is 365: ");"



    if yyyy is false
    {
    }
    else if is true
    {
    System.out.print("\n "The number of days in + (yyyy) + is 366 ");"
    }

    System.out.print("\nWould you like to continue? ( 0 = Yes ; 1 = No )");
    decision = input.nextInt ();


    }



    System.out.print("Good Bye");



    } // End Main
    } // End Class Leap Year



    Here is what I am trying to reproduce

    Please enter the desired year - 2000
    The number of days in 2000 is 366
    Do you wish to continue ( 0 = No ; 1 = Yes )? - 1


    Also if I want to add a second static class that is numberofdays, that will call to the main, any ideas?

    Thanks

  2. #2
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    First off, you aren't declaring a scanner anywhere. You need to create a scanner, or some method of input.

    Second, you cannot have a method within another method (boolean within main). You should move that to just inside the class.

    Third, your if's don't seem to be set up properly. If you're returning false as an else, then you won't need to return any ifs as false, unless if they are followed by an else if that will return true. Try:
    Java Code:
    if(nYear%4==0 && nYear%100!=0 || nYear%400=0)return true;
    else return false;
    And instead of checking if yyyy is false, just check if its true and don't worry about the else if.

    Fourth, you don't have a boolean that you're checking if it is false. You're just checking yyyy, which you have declared as a short inside of the boolean, but even yyyy isn't declared outside of the boolean method. Try:
    Java Code:
    Scanner in = new Scanner(System.in);
    int nYear = in.nextInt();
    boolean isLeapyear = isLeapYear(nYear);
    Hope this helps!

    ~Jamie

    P.S. - Please use code tags in the future! //Code Tags
    Last edited by dunafrothint; 03-04-2010 at 08:36 PM.

  3. #3
    lk1001 is offline Member
    Join Date
    Feb 2010
    Posts
    8
    Rep Power
    0

    Default

    Still cannot compile, but getting closer. Here is what I put together based on suggestions Jamie

    Java Code:
    public class Leapyear 
    {
    
    public static boolean main (String [] args)
    {
    
    	
    	   {
    final boolean isLeapYear(short nYear){
    	return false;
    }
    if(nYear%4==0 && nYear%100!=0 || nYear%400=0)return true;
    else return false;
    	   }
    	   Scanner in = new Scanner(System.in);
    	   int nYear = in.nextInt();
    	   boolean isLeapyear = isLeapYear((short) nYear);
    
           while (isLeapYear=true);
               Scanner Input= new Scanner(System.in);
               
                   System.out.print("Please enter the desired year - ");
                   nYear= input.nextShort();;
    
                   System.out.print("\n "The number of days in" + nYear + is 365: ");
    
    
    
                   if (isLeapYear=false);
                   {
                      System.out.print("\n "The number of days in" +nYear + is 366 ");
                   }
    
                           System.out.print("\nWould you like to continue? ( 0 = Yes ; 1 = No )");
                           decision = input.nextInt ();
    
    
           }
    
    
    
                   System.out.print("Good Bye");
    
    
    
    } // End Main
    } // End Class Leap Year
    Last edited by lk1001; 03-04-2010 at 08:48 PM.

  4. #4
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    You're still declaring a boolean method isLeapYear inside of main and now you have declared main as a boolean which requires you to return a boolean value to something. Here's some sample code I came up with. Try making your own based off of this:
    Java Code:
    package Testing;
    import java.util.*;
    public class BoredLol {
        public static void main(String[] args){
            Scanner sin = new Scanner(System.in);
            int nYear = 0, nRepeat=1;
            while (nRepeat>0){
                nYear=sin.nextInt();
                if (nYear%4==0&&nYear%100!=0||nYear%400==0)System.out.println("This is a leap year.");
                else System.out.println("This is not a leap year.");
                System.out.println("Again? (1 = Yes, 0 = No)");
                nRepeat=sin.nextInt();
            }
        }
    }

  5. #5
    lk1001 is offline Member
    Join Date
    Feb 2010
    Posts
    8
    Rep Power
    0

    Default

    Now I got it to work, but I have to convert to shorts and add another method.

    Here is what I have [CODE][import java.util.*;
    public class Main {
    public static void main(String[] args){
    Scanner input = new Scanner(System.in);

    System.out.println ("Please enter the desired year -");
    int nYear = 0, nRepeat=1;
    while (nRepeat>0){
    nYear=input.nextInt();
    if (nYear%4==0&&nYear%100!=0||nYear%400==0)System.out .println("The number of days in " + nYear + " is 366" );
    else System.out.println("The number of days in " + nYear + " is 365");
    System.out.println("Do you wish to continue ( 0 = No ; 1 = Yes )? - ");

    nRepeat=input.nextInt();
    System.out.println ("Please enter the desired year -");
    while (nRepeat<0.9){
    System.out.println("Goodbye");
    break;
    }
    }
    }
    }
    /CODE]

    Here is what I need to do, any suggestions?

    Aside from the main which re prompts the user for input, your program is to consist of two static methods. The first is static boolean isLeapYear( short year ). It accepts a short meant to represent a year and returns a boolean indicating whether or not the input year is or is not a leap year. This is quite trivial since your book already gives you an implementation for this method . The prototype type of the second method is static short numDaysInYear( short year ). As you might imagine, this method accepts as input a short representing a year and returns a short that is the number of days in that year. That number is 365 if the user input is not a leap year and 366 if it is. Therefore, the method numDaysInYear invokes or calls isLeapYear. Like most programs in any language, the main ( I often call it the driver ) simply contains a collection of calls to other methods ( the main is a method after all ). In this case, aside from prompting the user for input, it also calls numDaysInYear which itself calls isLeapYear.

  6. #6
    lk1001 is offline Member
    Join Date
    Feb 2010
    Posts
    8
    Rep Power
    0

    Default

    Any ideas guys? I have been trying diffrent ways to get these classes to work and keep striking out, any help would be appreciated.

    Here is the newest version

    Java Code:
    import java.util.*;
    public class test{
        public static void main(String[] args){
    
    
            //Scanner Input
            Scanner input = new Scanner(System.in);
    
            //User input of year
            System.out.println ("Please enter the desired year -");
        Scanner in = new Scanner(System.in);
    int yyyy = in.nextInt();
    int nRepeat=0;
    short isLeapYear=0;
        while (nRepeat>0)
        
        {
                
                if (isLeapYear=True):
                    System.out.println("The number of days in " + yyyy + " is" + numDaysInYear);
                else System.out.println("The number of days in " + yyyy + " is" + numDaysInYear);
                System.out.println("Do you wish to continue ( 0 = No ; 1 = Yes )? - ");
        }
                //Response to would you like to
                
                
                System.out.println ("Please enter the desired year -");
                nRepeat=input.nextInt();
                while (nRepeat<0.9){
                	System.out.println("Goodbye");
                	break;
        
        }
           
        }
        
        
        
        public static boolean isLeapYear ( short yyyy )
    {
    
    if ( yyyy % 4 != 0 ) return false;
    if ( yyyy % 100 != 0 ) return true;
    if ( yyyy % 400 != 0 ) return false;
    if ( yyyy % 3200 != 0 ) return true;
    return false;
    }
    
    public static short numDaysInYears( short yyyy )
    { if (isLeapYear=True);
      numDaysInYears=365;
      Else 
              numDaysInYears=366;
    Last edited by lk1001; 03-05-2010 at 01:24 AM.

Similar Threads

  1. Replies: 12
    Last Post: 01-17-2010, 11:05 PM
  2. compile ignoring dependency errors
    By Geoffrey in forum Java Servlet
    Replies: 1
    Last Post: 09-10-2009, 06:45 PM
  3. New to this - having errors with static problems
    By afisher300 in forum New To Java
    Replies: 4
    Last Post: 04-20-2009, 11:42 PM
  4. Help with Compile time errors
    By bri1547 in forum New To Java
    Replies: 2
    Last Post: 08-24-2008, 11:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •