File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Error in simple for loop inside a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error in simple for loop inside a method" Watch "Error in simple for loop inside a method" New topic
Author

Error in simple for loop inside a method

Fergal Crawley
Greenhorn

Joined: Nov 28, 2011
Posts: 18

I'm a beginner and am learning how to use methods. The IDE I'm using is NetBeans.

Although I've spent a lot of time looking through the short program below, I can't figure out what's causing the errors in lines 13 and 14.



Could you please give me some suggestions as to how I can figure out what is causing these errors? I believe that the error is something very simple and really what I want to learn is how to go about finding the cause of those errors, rather than a solution for this particular program.

The error message from Netbeans is as follows;

run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - not a statement
at liangch5.LiangCh5.sum(LiangCh5.java:14)
at liangch5.LiangCh5.main(LiangCh5.java:9)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)


Thanks!

Beginner
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Have a close look at your for statement - particularly the final part - and compare it to the for loop in the Java tutorials. You should notice a crucial difference - you've added something.
Fergal Crawley
Greenhorn

Joined: Nov 28, 2011
Posts: 18

Thanks Matthew, I see what I've done wrong by declaring the i variable at the end of the for statement. It was a stupid error that I just couldn't see.

Is there any way that I could have identified the exact cause of the error, from the error messages in Netbeans?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
No, that error message was too vague to give you the hint. It is actually difficult to write good error messages; Java7 is better than earlier versions for its error messages.
Fergal Crawley
Greenhorn

Joined: Nov 28, 2011
Posts: 18

Thanks Campbell, I'm sure most programmers would be glad to see more helpful error messages.

I spent some time looking at that code without seeing the error. Would anyone have any tips on how to identify errors like that quicker?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Don't worry on not seeing it. It took me some time to see it as well.
Eclipse will actually put a little red squiggly line underneath the pieces of code that cause compiler errors. I'm pretty sure Netbeans can do the same.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Eclipse has better error messages than the Oracle compiler.
Fergal Crawley
Greenhorn

Joined: Nov 28, 2011
Posts: 18

Thanks for the replies, I might consider switching to Eclipse at some time to see if it provides better error reporting.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

I had to copy it into eclipse to see the problem, in fact it underlined it for me.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Here are the error messages I see when using the javac compiler on the command line:
While the text of the messages isn't much more informative than what you already saw, it's worth noting that they do provide a little ^ character which points to the approximate location in the line where the error occurred. Or at least, where the compiler thinks the error occurred. This isn't entirely reliable, since when the compiler doesn't understand what you're doing, its guess about what you are trying to do may be badly inaccurate. Still, the ^ can provide useful clues about what part of the line is the problem. In this case, the error at least points to the "int i++" part of the statement.

I don't know if NetBeans preserves the ^ part of the message that the compiler is giving you. If not, that's an extra reason to switch to another means for compiling and running your programs. Even using javac from the command line may give you more information. But maybe if you look closely, NetBeans might actually have some sort of visual indicator about which part of the line is the problem, the way eclipse and IntelliJ do.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
I think you might be better using the command-line and a decent text editor, leaving IDEs until you are more experienced.
Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 498
Also, the sum function is not generalized. It will not work when k<j and for -ve inputs (j=-1 and k=-10). >
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Also note that javac is giving three error messages for the price of one. If that happens, it is always worth looking at what appears to be the first error (marked by the caret symbol ^), correcting that and re-compiling.
Fergal Crawley
Greenhorn

Joined: Nov 28, 2011
Posts: 18

Thanks everyone. Unfortunately my IDE doesn't show the " ^" symbol. I've been using an IDE for a little while now and find it easier than using a text editor and command line, so will probably stick with it at least for the moment.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
OK, but this suggests that NetBeans is a relatively crappy IDE if it loses the location info associated with those error messages. I think Eclipse or IntelliJ may serve you better in the long run. I've never used NetBeans much, but I can recommend either Eclipse or IntelliJ (I favor IntelliJ).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in simple for loop inside a method
 
Similar Threads
Evaluation of nested loops
I am not getting the required output...
loop
ArrayIndex OutOfBoundsException?
how does this program work