0

Say I have the following block of code somewhere:

while(condition){
    try {
        String twoStr= JOptionPane.showInputDialog("Enter two words sep by a space.");
        \\do stuff like splitting and taking an index which may cause out of bound exception
    catch (IOException e) {
        System.out.println("Sorry your words are terrible.");
     }

\\do stuff with twoStr

I cannot access string outside of the try/catch statement, but I don't want to prompt the user to enter another two words in case he messes up and enters a single word. How can I access this value that was initialized in the try block?

1
  • It's a local variable, move the declaration out of try/catch. Commented Sep 26, 2013 at 6:35

8 Answers 8

6

The string should be defined outside the try/catch statement:

while(condition){
    String twoStr = null;

    try {
        twoStr= JOptionPane.showInputDialog("Enter two words sep by a space.");
     } catch (IOException e) {
        System.out.println("Sorry your words are terrible.");
     }

     //do stuff with twoStr

In this way, both the try/catch statement, as well as the code outside it, will see the variable. You should read up on scopes to get a better understanding of this.

Sign up to request clarification or add additional context in comments.

Comments

2

Change the scope of twoStr

while(condition){
String twoStr= null;
    try {
         twoStr= JOptionPane.showInputDialog("Enter two words sep by a space.");
        \\do stuff like splitting and taking an index which may cause out of bound exception
    catch (IOException e) {
        System.out.println("Sorry your name is bad.");
     }

\\do stuff with twoStr

1 Comment

I could get nit picky and point out that this won't compile ;)
2

When you define the string inside the try catch block, it is local to only that block and will not be accessible outside. Define the String variable outside the try catch block like this-

while(condition){
String twoStr;
try {
    twoStr= JOptionPane.showInputDialog("Enter two words sep by a space.");
    //do stuff 
 }catch (IOException e) {
    System.out.println("Sorry your name is bad.");
 }
.....

3 Comments

yeah fixed the comment also, thanks :) Now there is no compiler error
Still won't compile ;) (not for the purpose that the OP wants it for at least...)
Uh-uh, hint, try using the twoStr value after the try-catch ;)
2

Declare the String outside try catch. If you declare a variable inside the try catch,the scope of the variable is for inside the try catch only So you can not use it outside.

 while(condition){
     String twoStr =null;
    try {
        twoStr= JOptionPane.showInputDialog("Enter two words sep by a space.");
       //do stuff like splitting and taking an index which may cause out of bound exception
       }
    catch (IOException e) {
        System.out.println("Sorry your words are terrible.");
     }

1 Comment

@MadProgrammer: LOL :) Next time i will take care while copying OP's code :)
0
String twoStr = null;
while(condition){
     try {
        twoStr = JOptionPane.showInputDialog("Enter two words sep by a space.");
        \\do stuff like splitting and taking an index which may cause out of bound exception
    catch (IOException e) {
        System.out.println("Sorry your name is bad.");
     }

\\do stuff with twoStr

Comments

0

twoString is defined within the context of the try-catch block, therefore it can't be accessed out side it, instead you need define the variable before the try-catch block...

while(condition){
    String twoStr = null;
    try {
        twoStr = JOptionPane.showInputDialog("Enter two words sep by a space.");
        //do stuff like splitting and taking an index which may cause out of bound exception
    } catch (IOException e) {
        System.out.println("Sorry your name is bad.");
    }

    // You now have access to twoStr

Comments

0

Just declare it outside of try-block:

String twoStr = null;
while(condition){
    try {
        twoStr= JOptionPane.showInputDialog("Enter two words sep by a space.");
        \\do stuff like splitting and taking an index which may cause out of bound exception
    catch (IOException e) {
        System.out.println("Sorry your words are terrible.");
     }

\\do stuff with twoStr

1 Comment

I'd add "why" and fix the last line so it would be close to compiling ;)
0

You cannot access the string variable String twoStr outside the try catch block because the scope of the variables declared within the try/catch block are not in scope in the containing block, for the same reason that all other variable declarations are local to the scope in which they occur. You need to declare that variable out of the try catch block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.