2012-02-18 5 views
-1

簡単な質問。これを可能にする方法はありますか?アレイの初期化中にキャッチしよう

int array [] = new int [ 
    (try { 
      Integer.parseInt (/*get string input here*/); 
      } 
    catch (NumberFormatException e){ 
    //error handling here 
    ) 
]; 
+4

を参照してください。配列を初期化する前に*行います。 –

答えて

0

これは法的な構文ではありません。

int number; 
try 
{ 
    number = Integer.parseInt (/*get string input here*/); 
} 
catch (NumberFormatException e) 
{ 
    //error handling here 
} 

int array [] = new int [2]; 
array[0] = number; 

これは少し冗長ですが、Javaにようこそ。

0

Like Brian said ..あなたのtry/catchブロックを配列の初期化の上に移動する必要があります。

int i; 

try{ 
    string myString = "1"; 
i = Integer.parseInt(myString)l 
} catch(NumberFormatException e){ 
    //handle 
} 

int array [] = new int[3]; 
0

はい...以下

int myArray[] = new int[10]; 
String tempString = "1"; 
try { 
    for (int i=0;i<=9;i++) { 
     tempString += i; 
     myArray[i]=Integer.parseInt(tempString); 
    } 
} catch (NumberFormatException nfe) { 
    // print error 
} catch (Exception e) { 
    // print error 
} 
関連する問題