aspose file tools*
The moose likes Beginning Java and the fly likes Splitting an input string whilst storing its value beyond a block of code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Splitting an input string whilst storing its value beyond a block of code" Watch "Splitting an input string whilst storing its value beyond a block of code" New topic
Author

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

Shamsudeen Akanbi
Ranch Hand

Joined: Dec 24, 2010
Posts: 72
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

Joined: Oct 02, 2003
Posts: 11318
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Shamsudeen Akanbi
Ranch Hand

Joined: Dec 24, 2010
Posts: 72
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

Joined: Aug 16, 2005
Posts: 14156
    
  19

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18846
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Harsha Ka
Ranch Hand

Joined: Mar 14, 2010
Posts: 45

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.



SCJP 1.6,Preparing (Tryin to prepare) for scwcd
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11318
    
  16

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

Joined: Dec 24, 2010
Posts: 72
@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

Joined: Oct 02, 2003
Posts: 11318
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Splitting an input string whilst storing its value beyond a block of code