Michael Romero wrote:Your array declaration says 'make an array containing other int arrays'.
So assigning a new array of integers makes sense: 'new int[]'.
But in your second example you try to assign an integer ('2') to an array that can only hold integer arrays.
Stephan van Hulst wrote:Given that square is a 2D array, it seems it shouldn't be possible to assign a value to the array without explicitly including both dimensions (e.g. int[2][1]).
That's because square is not a 2D array. It's an array of arrays. Java does not have higher dimensional arrays.
manish ghildiyal wrote:
I removed try/catch blocks and got no compiler error.Program ran with output 1, just as was expected.
Manish
Matthew Brown wrote:The question is, what do you want to actually happen if an exception is thrown? You say you think it's risky to provide a temporary value - but in that case surely it's risky to initialise it to a temporary value as well?
Oh, and I know this is just test code, but I'd recommend getting into the habit of never writing catch(Exception) {}. The catch block should never be empty, because that way you've got absolutely no idea what happened if it goes wrong. Print or log the stack trace at least. I've seen plenty of questions in the forum from people asking why their code doesn't work, when the computer is telling them why it doesn't work and they've chosen to ignore it! In the very few occasions where you do need to entirely ignore an exception, put a comment in the block stating this.
Enter a number between 0 and 1:
5.0
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at test.main(test.java:7)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Process finished with exit code 1
Thanks!Jesper de Jong wrote:Welcome to the Ranch!