This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning a string from a method

 
Jean Luchau
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeremy Wilson
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jean Luchau
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic