• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I fix this code? I don't understand the error messages.  RSS feed

 
Charles Ellis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a Leap year assignment that requires that I implement a public class Date and a class named DateJDialog.java.Here is what I wrote:







Date.java compiles correctly. DateJDialog.java gives me the following compliation errors:

DateJDialog.java:21: error: variable d is already defined in method main(String[])
Date d = new Date(m,d,y);
^
DateJDialog.java:23: error: int cannot be dereferenced
boolean b = d.isLeapYear(y);
^
DateJDialog.java:25: error: cannot find symbol
JOptionPane.showMessageDialog(null," If someone said the date " + m + "/" + d + "/" + y + "is a leap year , that statement would be " + b + ".");
^
symbol: variable b
location: class DateJDialog
3 errors

Can anyone help me? I hope I've obeyed the forum rules.

Thanks,
captgbv
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically it says, that variable "d" is already defined somewhere in your code and you're trying to do so again.
Check carefully your code again. For example (code not intended to do nothing, just an example):
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, when you define variable:
Variable "i" is initialised within "for" loop on line 1, as well as on line 4. Is this code correct? Why?

What would be if you would try:
What would be the output?

I'm referring all code above to your code lines 22, 23, 24, 25 of your DateJdialog class.

And welcome to JavaRanch
 
Charles Ellis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THank you for the quick response. So, how do I fix it? Do I rename the boolean variable?
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Ellis wrote:THank you for the quick response. So, how do I fix it? Do I rename the boolean variable?

Always start by fixing 1st error compiler gives you. How to fix, you should try to figure out yourself first. I think I gave you quite accurate examples, so you should be able to work out.
Write down your thoughts, where do you think your problems are? Why do you think these are problems? How would you fix them? And why do you think it should work?

P.S. guesses are not correct way to solve problems.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Ellis wrote:THank you for the quick response. So, how do I fix it? Do I rename the boolean variable?

The problem with your boolean is not the name but that you've declared the variable inside a block ({}) and it goes out of scope (disappears) when you reach the end of the block.
As a general comment I would suggest more descriptive variable names.
 
Charles Ellis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help. I finally see how to fix it. I renamed d to d1 and it fixed it. In the future I will use more descriptive variable names. Thanks again for your help.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:

Careful, this is true only if the first n is local (not instance). Also instead of initialized you probably mean instantiated? It doesn't have to be initialized to cause the problem.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:Careful, this is true only if the first n is local (not instance). Also instead of initialized you probably mean instantiated?

1. If "n" were an instance variable, "if" statement couldn't be outside the method.
2. I didn't mean instantiated. I meant what I wrote. But in order to get an error, would be enough to declare without initialisation. But I was right based on my given example.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!