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 Returning a string from 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 "Returning a string from a method" Watch "Returning a string from a method" New topic
Author

Returning a string from a method

Jean Luchau
Greenhorn

Joined: Mar 05, 2008
Posts: 2
Hi,

I keep getting an error (cannot resolve symbol) when compiling my code. How can I get my method to return the concatonated string strAbsentData ?



best wishes!
[ March 05, 2008: Message edited by: Jean Luchau ]
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Welcome to Javaranch.

Do you understand what "scope of a variable" means? To put it very simplistically, any variable that declared is visible only in certain parts of the code. This is known as its scope. Typically, a variable's scope is restricted to the *block* that it is defined to. By "block", I mean the portion of code that is between { and }.

You have declared "strAbsentData" within a for loop. You try to return a "strAbsentData" well outside of this for loop. The compiler has no way of knowing that this "strAbsentData" that you are trying to return is the same "strAbsentData" that you have declared within the for loop.

As such, I would recommend that you spend sometime learning the basics from either a good tutor or a good book, before you move on to dabbling with Java code.

- Anand


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Jeremy Wilson
Ranch Hand

Joined: Feb 18, 2003
Posts: 166
declare the

String strAbsentData variable as the first line in the function, not deep in the body. Take a look at scope rules for an explanation.


Jeremy Wilson
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

the error here is due to the string declaration being confined to the try block and the variable is not visible out of try block during the return statement.
One way to resolve this error is to declare it as a method local variable by declaring it at the beginning of the method and initializing it.


-kolli
Jean Luchau
Greenhorn

Joined: Mar 05, 2008
Posts: 2
Thanks Jeremy, that fixed it.

To those I seem to have offended by asking this simplistic question - my apologies. I guess the word "beginner" means different things to different people.

Originally posted by Jeremy Wilson:
declare the

String strAbsentData variable as the first line in the function, not deep in the body. Take a look at scope rules for an explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning a string from a method
 
Similar Threads
login with Struts2&MySQL
Problem while inserting data into MS SQL table
ClassNotFoundException Mysql
ClassNotFoundException Mysql
Help with this code!