• Post Reply Bookmark Topic Watch Topic
  • New Topic

code won't compile  RSS feed

 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody.

Could someone please help me understand why my testCode won't compile.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, please TellTheDetails (←click that, it's a link) when you are asking about errors you encounter when compiling or running your program. Copy/paste the actual error messages so that people will have a better idea of what you're dealing with.

The main reason for your problem is that on line 5, the main() method has a an open brace "{" at the end of the line that has no matching closing brace, "}" before line 7 where you start defining another method.

The way it's written, the Java compiler won't give you the most helpful error message. In Eclipse, I see a messages about "void is an invalid type for the variable tomorrow" and "syntax error on token '('" which is understandable, if you know how a compiler "thinks." Most people, however, don't have intimate knowledge about how a compiler reports the reasons for errors.

Again, the real reason for the compiler error is that the closing brace that you have on line 18 should really be moved to line 6.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing that you did well, however, was to more or less properly align and consistently indent your code.  Because of this, the misplaced closing brace on line 18 stands out because it is not aligned properly and it is out of place.

The lesson here is that you should always properly align and indent your code so that the structure and organization of your program is clear. Proper alignment and indentation often helps you find logic errors much quicker.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see you have methods inside of main. Im not much of an expert but i dont believe that is allowed. If im wrong, someone will prob correct me faster than i can say main method.
 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am starting to see my errors here.

I am allowed to have multiple methods in the same class though, right. Am I able to run those methods by just running that class, or must I call the method .tomorrow();
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not allowed to define methods directly inside methods. If you fix your code the way I advised previously, there will be no code inside the main() method. You'd have to put something in there that can be executed.

Your other two methods, tomorrow() and addOne() are defined as instance methods. That means they can only be called via a reference to an instance of the testCode class. BTW, the name testCode does not conform to standard Java naming convention. Class names should start with a capital letter so it should be written as TestCode instead.  Style-wise, it's poor form to name a class something like TestCode.  The name is meaningless and does not say anything about what the code in it is really doing.  You might as well call the class a ThingamaJig or a Foobar.  Give it a name that gives you an idea of what the class is for, besides testing out some code.
 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what parameter should i pass when calling the method tomorrow ?

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will Dev wrote:what parameter should i pass when calling the method tomorrow?

See the API documentation for java.util.Calendar (←click that link) for different ways you can create a Calendar object. Since you declared that method to take a Calendar object as its argument, that's what you'll have to pass to it.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop using Calendar. Find out about the new new date and time classes, which are much better.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!