• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic