UPDATE: sorry for misleading some of you, here is more detailed code:
I'm trying to parse String[]
array to int[]
array. I take String from JTextArea
, make String[]
and then int[]
.
array1String="asd, lala, 22";
array2String="1,2,3";
String [] arr1 = array1String.split(",");
String [] arr2 = array2String.split(",");
int[] array1 = new int[arr1.length];
for (int i=0; i<arr1.length; i++) {
try {
array1[i] = Integer.parseInt(arr1[i]);
} catch (Exception ex) {
resultLabel.setText(ex.getMessage());
}
}
int[] array2 = new int[arr2.length];
for (int i=0; i<arr2.length; i++) {
try {
array2[i] = Integer.parseInt(arr2[i]);
} catch (Exception ex) {
resultLabel.setText(ex.getMessage());
}
}
the parseInt()
method interprets any gibberish as a "0". So instead of getting an exception, I get every int[] member corresponding to gibberish String[] member as a ZERO. But that's not what I want. I want to catch the exception to pass the message to the JLabel. I don't want it to happily eat any nonsense and make it "0". Am i missing something? I suspect that is not how this is supposed to work.
result arrays look like the
array1 [0,0,22]
array2 [1,2,3].
And nothing goes to Label, as if exception never happened. By i didn't change the code, only handling the exception changed.
The default value for int is 0. Since you get an exception and nothing is assigned to x, you get 0