如何在Java中将锯齿状数组解析为单个变量?

dww*_*n66 0 java arrays parsing

我有一个至少有三个元素的锯齿状数组,我需要解析前五个元素,用空格填充任何空值.

 // there will ALWAYS be three elements 
String whiconcatC = scrubbedInputArray[0];
String whiconcatD = scrubbedInputArray[1];
String whiconcatE = scrubbedInputArray[2];

 // there MAY be a fourth or fifth element
if (scrubbedInputTokens > 3) {
String whiconcatF = scrubbedInputArray[3];
} else {
String whiconcatF = " ";
}
 //
if (scrubbedInputTokens > 4) {
String whiconcatG = scrubbedInputArray[4];
} else {
String whiconcatG = " ";
}
Run Code Online (Sandbox Code Playgroud)

虽然上面的代码在编译期间不会产生错误,但后续行引用whiconcatFwhiconcatG在编译期间会出错cannot find symbol.

我已经尝试使用forEachStringTokenizer(在将数组转换为分隔的字符串之后),但无法弄清楚如何在实例中使用默认值,即第4和第5点没有值.

我无法找出任何其他方法来做到这一点,也不知道为什么我的逻辑失败了.建议?

Ank*_*agi 5

那是因为它们具有局部范围并且在括号内定义.因此,当您关闭括号并且无法访问时,模具会死亡.在外面定义它们你应该没问题.

String whiconcatC = scrubbedInputArray[0];
String whiconcatD = scrubbedInputArray[1];
String whiconcatE = scrubbedInputArray[2];
String whiconcatF = "";
String whiconcatG = "";


// there MAY be a fourth or fifth element
if (scrubbedInputTokens > 3) {
whiconcatF = scrubbedInputArray[3];   
} else {
whiconcatF = " ";
}
//
if (scrubbedInputTokens > 4) {
whiconcatG = scrubbedInputArray[4];
} else {
whiconcatG = " ";
}
Run Code Online (Sandbox Code Playgroud)