Results 1 to 19 of 19
Thread: Help with Leap Year program
- 11-28-2012, 11:34 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
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);
}
}
- 11-29-2012, 06:34 AM #2
Re: Help with Leap Year program
Please go through http://www.java-forums.org/forum-gui...w-members.html and BB Code List - Java Programming Forum and edit your post accordingly.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 11-29-2012, 11:06 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 11-30-2012, 02:38 AM #4
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
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?
- 11-30-2012, 12:33 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 11-30-2012, 05:54 PM #6
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Re: Help with Leap Year program
Ohhhhhhh okay I gotcha. Thanks for the help!
- 12-02-2012, 09:11 PM #7
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
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 :(
-
Re: Help with Leap Year program
Please show us your best attempt so that we can see what misconceptions, if any, you may have.
- 12-02-2012, 09:38 PM #9
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Re: Help with Leap Year program
Wouldn't I use this?
Java Code:public String toString() { return "["+getContact()+"]";
-
Re: Help with Leap Year program
- 12-03-2012, 08:10 PM #11
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
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); } } }
Basically I just need to get the box object converted into a string to display in the output message, right?
-
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.
- 12-03-2012, 11:35 PM #13
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Re: Help with Leap Year program
I still get "Illegal start of expression" error messages...I must be totally off
-
Re: Help with Leap Year program
- 12-04-2012, 03:52 AM #15
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
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); } } }
<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!
-
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); } } }
// 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.
-
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.
- 12-04-2012, 04:22 AM #18
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
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.Website: Learn Java by Examples
- 12-04-2012, 08:13 PM #19
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Leap Year Test: Help!
By Zigster in forum New To JavaReplies: 5Last Post: 04-23-2012, 06:02 AM -
Determing leap year using "if statement"
By jdm113497 in forum New To JavaReplies: 7Last Post: 04-12-2011, 07:49 PM -
Leap Year Calculator
By Pkaay in forum New To JavaReplies: 9Last Post: 10-12-2010, 11:47 PM -
Having problem in calculating leap year
By lclclc in forum New To JavaReplies: 3Last Post: 09-25-2009, 09:50 PM -
Leap Year Program
By busdude in forum New To JavaReplies: 3Last Post: 10-16-2008, 04:46 AM
Bookmarks