Results 1 to 19 of 19
  1. #1
    newtojavahelp is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Help with Leap Year program

    Hello everyone I need help with my leap year program. I'm suppose to have two separate documents, one to decide which is a leap year and the other to display if its true or false using joptionpane. I think my problem is with the object I created. Any help is appreciated!!!

    Here is my error: error: incompatible types
    if (box)
    ^
    required: boolean
    found: LeapYear
    1 error


    Here is my code: import javax.swing.JOptionPane;
    public class LeapYearJDialog
    {
    public static void main(String[] args)
    {
    String year; // Hold the inputed year.
    String result; // Hold the conversion result.

    // Create a year object.
    LeapYear box = new LeapYear( ) ;

    // Get the input year.
    year = JOptionPane.showInputDialog("Input A Year>>");
    double heightDouble = Double.parseDouble(year);

    // Display result.
    if (box)
    JOptionPane.showMessageDialog(null, "Conversion Result:" + " True");
    else
    JOptionPane.showMessageDialog(null, "Conversion Result:" + " False");

    System.exit(0);

    }
    }



    public class LeapYear
    {
    private boolean box;

    public boolean isLeapYear(int year)
    {
    // tests whether a year is a leap year or not
    if (year%4==0 && year%100!=0 || year%400==0)
    box = true; //remember for later if leap year or not
    else
    box = false;
    return (year != 0);
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Help with Leap Year program

    Please go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Help with Leap Year program

    The 'box' variable in your main() method is a LeapYear object, not a boolean.
    It probably ought to be called 'leapYear' (or something).
    I think you actually want to be calling the isLeapYear() method on it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    newtojavahelp is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help with Leap Year program

    Thanks Tolls

    I'm still getting a funky output message, like "Conversion Result: LeapYear@15bfdbd, after a year is inputted.

    Sorry about the thread errors...how do I edit it or do I have to make a new thread?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Help with Leap Year program

    That's because you haven't overridden the default toString() method.
    That just prints the class name and memory address.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    newtojavahelp is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help with Leap Year program

    Ohhhhhhh okay I gotcha. Thanks for the help!

  7. #7
    newtojavahelp is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help with Leap Year program

    How do you override the toStringmethod? I've searched but everything thing I've found doesn't work or help..unless I'm missing something. I'm just lost :(

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with Leap Year program

    Please show us your best attempt so that we can see what misconceptions, if any, you may have.

  9. #9
    newtojavahelp is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help with Leap Year program

    Wouldn't I use this?
    Java Code:
    public String toString() {
        return "["+getContact()+"]";
    I've tried but i get an "Illegal start of expression" error from the public part...I feel like I'm missing one little thing.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with Leap Year program

    Quote Originally Posted by newtojavahelp View Post
    Wouldn't I use this?
    Java Code:
    public String toString() {
        return "["+getContact()+"]";
    I've tried but i get an "Illegal start of expression" error from the public part...I feel like I'm missing one little thing.
    Could you be declaring this method inside of another method? Please show the rest of this class if you can.

  11. #11
    newtojavahelp is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help with Leap Year program

    Here is the whole code with the "public String toString" method

    Java Code:
    //***********************************************************
    // Assign5.java             Author: Travis Rippy
    ////*********************************************************
    
    import javax.swing.JOptionPane;
    import java.io.*;
    
    /**
     *  This program demonstrates using dialogs
     *  with JOptionPane by implenting the GUI
     *  to decide if an inputed year is a leap
     *  year or not.
     */
    
    public class LeapYearJDialog
    {
       public static void main(String[] args)
       {
          String year;                 // Hold the inputed year.
    
    	  // Create a year object.
             LeapYear leapyear = new LeapYear();
    
          // Get the input year.
             year = JOptionPane.showInputDialog("Input A Year>>");
    		 double yearDouble = Double.parseDouble(year);
    
    		 leapyear.toString();
    		 public String toString() {
        		return "["+leapyear()+"]";
    
    	  // Display result.
    
    	   		   JOptionPane.showMessageDialog(null, "Conversion Result:" + leapyear);
    
                System.exit(0);
       }
    }
    }
    I get the Illegal start of expression pointing at 'public' but thats from the public static void so is there an alternative to this method?

    Basically I just need to get the box object converted into a string to display in the output message, right?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with Leap Year program

    It's just as I suspected: you're creating the toString method inside of another method (here the static main method), and you can't do this with Java. Instead place the toString method in the class and not nested in any other method.

  13. #13
    newtojavahelp is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help with Leap Year program

    I still get "Illegal start of expression" error messages...I must be totally off

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with Leap Year program

    Quote Originally Posted by newtojavahelp View Post
    I still get "Illegal start of expression" error messages...I must be totally off
    I can't tell without code. Please show us your latest code attempt, and again indicate which line is stimulating this error.

    Don't give up -- you'll be able to figure this out.

  15. #15
    newtojavahelp is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help with Leap Year program

    Java Code:
    //***********************************************************
    // Assign5.java             Author: Travis Rippy
    ////*********************************************************
    
    import javax.swing.JOptionPane;
    import java.io.*;
    
    /**
     *  This program demonstrates using dialogs
     *  with JOptionPane by implenting the GUI
     *  to decide if an inputed year is a leap
     *  year or not.
     */
    
    public class LeapYearJDialog
    {
    	leapyear.toString();
    	public String toString() {
        return "["+leapyear()+"]";
    
       // Create a year object.
       static LeapYear leapyear = new LeapYear();
    
    	 public static void main(String[] args)
       {
          String year;                 // Hold the inputed year.
    
          // Get the input year.
             year = JOptionPane.showInputDialog("Input A Year>>");
    		 double yearDouble = Double.parseDouble(year);
    
    		 leapyear.toString();
    
    	  // Display result.
    
    	   		   JOptionPane.showMessageDialog(null, "Conversion Result:" + leapyear);
    
                System.exit(0);
       }
    }
    }
    I get the following errors...

    <identifier> expected
    leapyear.toString(); - line 17
    ^

    error: illegal start of expression
    static LeapYear leapyear = new LeapYear(); - line 22

    error: illegal start of expression
    public static void main(String[] args) - line 24
    ^

    error: illegal start of expression
    public static void main(String[] args) - line 24
    ^

    error: ';' expected
    public static void main(String[] args) - line 24
    ^

    '.class' expected
    public static void main(String[] args) - line 24
    ^

    ';' expected
    public static void main(String[] args) - line 24
    ^

    Thanks for all of the help and guidance!

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with Leap Year program

    Let's look at your code:

    Java Code:
    public class LeapYearJDialog
    {
        leapyear.toString(); // A
    
        public String toString() { // B
        return "["+leapyear()+"]";
     
       // Create a year object.
       static LeapYear leapyear = new LeapYear();  // C
     
         public static void main(String[] args)
       {
          String year;                 // Hold the inputed year.
     
          // Get the input year.
             year = JOptionPane.showInputDialog("Input A Year>>");
             double yearDouble = Double.parseDouble(year);
     
             leapyear.toString();
     
          // Display result.
     
                   JOptionPane.showMessageDialog(null, "Conversion Result:" + leapyear);
     
                System.exit(0);
       }
    }
    }
    Regarding the lines that I've indicated:

    // A: what is the purpose of that line. You appear to be trying to call a method naked in the class, outside of any method or constructor. This can't be done.

    // B: You are declaring your toString() method here. When you create methods, for loops, if blocks, you'll use curly braces, and they come with a symmetry: all opening curly braces must have a closing brace, and this makes sense since the opening curly brace tells the compiler that "here is the start of my method body", and the closing one states, "here is the end of my method's body". Have you checked that all opening braces match closing braces? Here is where code formatting is supremely important for if you formatted your code well with clean and consistent indentation, you can tell at a glance whether this is so.

    // C: What is the purpose of this line? I see no need to declare a static variable in the class.

    Finally, consider scrapping this code (it's very short any way) and starting over, but using a different over-all coding technique. You should start with your class code skeleton, just the class declaration and the class's opening and closing curly braces, and then compile this skeleton code to make sure it compiles. Then add your code, one or two lines at a time, but compile the code after each one or two lines and make sure that it compiles without error. Here is the key -- do not add any new code until you have fixed all compilation errors. Doing this will help prevent you from trying to add good code to already bad code and ending up with a rat's nest of errors. Later as you get more familiar with Java, you'll likely use an IDE which will do this process for you -- that is, it will compile the code on the fly as you add each item into your program and will flag compilation errors immediately.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with Leap Year program

    Can you post your actual assignment requirements? I wonder if you even need a toString() method or if all can be done in main with maybe one or two static methods. You may be doing too much here.

  18. #18
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Help with Leap Year program

    I suggest that you, if you haven't read this The Java™ Tutorials, please read it. If you have read it please read it again. It is a good think to understand the very basic before you take more steps.

    Now, to your error.

    1. On line 17 you cannot just call leapyear.toString(). It should be called inside a block or assign the result to a variable.
    2. On line 22. You can see that before that line you've define a toString() method. A method definition should be inside curly braces. You only have an open braces but you haven't close it. The expression leapyear() is also not valid.
    3. The other error is caused by the above errors.

  19. #19
    newtojavahelp is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help with Leap Year program

    Ahhh i see, Thanks Fubarable! The assignment is to create a LeapYear class to decide whether an inputted year is a leap year or not and then display the result in another class, LeapYearJDialog, in dialog boxes(JOptionPane).

Similar Threads

  1. Leap Year Test: Help!
    By Zigster in forum New To Java
    Replies: 5
    Last Post: 04-23-2012, 05:02 AM
  2. Determing leap year using "if statement"
    By jdm113497 in forum New To Java
    Replies: 7
    Last Post: 04-12-2011, 06:49 PM
  3. Leap Year Calculator
    By Pkaay in forum New To Java
    Replies: 9
    Last Post: 10-12-2010, 10:47 PM
  4. Having problem in calculating leap year
    By lclclc in forum New To Java
    Replies: 3
    Last Post: 09-25-2009, 08:50 PM
  5. Leap Year Program
    By busdude in forum New To Java
    Replies: 3
    Last Post: 10-16-2008, 03:46 AM

Tags for this Thread

Posting Permissions

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