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?

4

8 回答 8

5

字符串应该在 try/catch 语句之外定义:

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

这样,try/catch 语句以及它之外的代码都将看到该变量。您应该阅读范围以更好地理解这一点。

于 2013-09-26T05:50:57.957 回答
2

在 try catch 之外声明字符串。如果你在try catch里面声明了一个变量,这个变量的作用域是只在try catch里面,所以你不能在外面使用它。

 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.");
     }
于 2013-09-26T05:52:33.323 回答
2

当您定义块string内部时try catch,它仅对该块是本地的,并且无法在外部访问。像这样在 try catch 块之外定义 String 变量-

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.");
 }
.....
于 2013-09-26T05:51:30.120 回答
2

改变范围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
于 2013-09-26T05:51:32.687 回答
0

只需在 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
于 2013-09-26T05:52:32.277 回答
0

您无法访问String twoStrtry catch 块之外的字符串变量,因为在 try/catch 块中声明的变量的范围不在包含块的范围内,原因与所有其他变量声明对于它们所在的范围都是本地的一样发生。您需要在 try catch 块之外声明该变量。

于 2013-09-26T05:55:23.213 回答
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
于 2013-09-26T05:51:11.447 回答
0

twoString是在块的上下文中定义的try-catch,因此不能在块之外访问它,而是需要在try-catch块之前定义变量...

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
于 2013-09-26T05:51:47.663 回答