Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Splitting an input string whilst storing its value beyond a block of code

 
Shamsudeen Akanbi
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers, i'm trying to split a line input, and I want it to update a variable if that's possible. But the scope seems limited to the try block. Here's my code:
 
fred rosenberger
lowercase baba
Bartender
Posts: 12124
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it it not clear what exactly the problem is. You do realize that the "token" variable declared on line 5 masks the one declared on line 3?
 
Shamsudeen Akanbi
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiling this program generates an output that, the variable token has not been initialised. So, I was thinking of a way of updating token from the try block. Because printing token in the try block ain't a problem. I hope you understand...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15283
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose you run this program.

In line 5 you declare a local variable token, but you do not initialize it.
Suppose that in line 7 an exception occurs.
Execution jumps to line 12, where the stack trace of the exception is printed.
Then line 14 is executed. Note that the variable token has not yet been initialized!

In Java, it is required to initialize local variables before using them. Whenever the compiler finds a code path where it is possible to use a local variable without initializing it first (such as described above), you get an error from the compiler.

Note that the member variable, declared in line 3, is not used at all in your program. The local variable declared in line 5 hides it.
 
Henry Wong
author
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shamsudeen Akanbi wrote:Compiling this program generates an output that, the variable token has not been initialised. So, I was thinking of a way of updating token from the try block. Because printing token in the try block ain't a problem. I hope you understand...



Well, that compile error is something completely different -- and not related to scope as your topic suggest.

Take a look at your code again -- and you will see that there is a possible path of execution in your code that will try to use the variable before it is initialized (exactly what the compiler says).

[EDIT: wow, that was quick. Not only beaten to the answer, but with a detailed one.]

Henry
 
Harsha Ka
Ranch Hand
Posts: 45
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler error is because Java forces all the local variables to be initialized before you can access them.Since there is a possibility that the try-block can throw exception, the "token" variable might not have been initialized. You can try initializing token to null.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12124
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your String token indeed may not be initialized. Think about what happens if you throw an exception on line 7 - what has token been initialized to?


A simple fix to the immediate problem would be to change line 5 to

or some other default value...but I still think you probably have a mistake by declaring a new String token inside your main method that masks the String token member variable. I could be wrong, but your going to have a hard time convincing me of that fact.
 
Shamsudeen Akanbi
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@fred, you're very right, it was because I wasn't getting any closer I had to declare it there, anyway thank you very much guyz, It's because I never initialized the variable token, its now up and running. Thank you all!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12124
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shamsudeen Akanbi wrote:it was because I wasn't getting any closer I had to declare it there

No offense intended, but that is really not a good way to code. You should strive to understand what the compiler is telling you. Making semi-random changes, hoping it fixes your problem is not going to lead to a good final program.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic