Jonathan Wallace

Greenhorn
+ Follow
since Aug 18, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jonathan Wallace

Are you guys sure?

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.



The declaration part is only "int[][] square" which would seem to create a 2D array. For instance, the following compiles:

----

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.



What would you consider to be a 2D array? The example above (square[0][0]) seems very much 2D to me. Then 3D arrayName[][][] and so on.
10 years ago
The following compiles:

while the following doesn't:

What I don't understand is:

1) Why does the first example compile at all? 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]).

2) How come the second example doesn't compile, when the assignment is done the exact same way?
10 years ago
Edit: Eh nevermind, seems the only problem was I was trying to use the *support* version: getSupportParentActivityIntent. Remove "support" and it works fine.


I need to be able to control dynamically to which activity the user goes "back", when they press the "Up" button.

It can be set statically in the manifest as follows:

It appears it is can be set programatically following the description here, but I can't seem to figure out how it works. Help anyone?
10 years ago
Nevermind... it turns out the error wasn't there, but at a different place...
10 years ago
I'm following a tutorial on android in Android Programming where I'm supposed to type the following two lines:

The first line works fine, but when I add the second, setListAdapter gives me "Return type for the method is missing", and adapter gives me "adapter cannot be resolved to a type". Imports are as follows:

Anyone know what's wrong?
10 years ago
I deleted and then imported an android project that had previous worked. It now crashes immediately and gives me the information below. Can anyone tell me what this means and how to interpret the info? Thank you!

10 years ago

manish ghildiyal wrote:

I removed try/catch blocks and got no compiler error.Program ran with output 1, just as was expected.

Manish



The problem is about catch try though. That is, how do you use a return value recieved within the try block, in the rest of the function.
10 years ago
Allright, I think I've got it. Thank you!
10 years ago

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?



Hmm you're right, the logic is flawed. However, putting that aside for the moment, I suppose the question I'm getting as it how I can use the 'y' value returned by riskyMethod within the main method, but outside the catch try block. 'Best practice'?

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.



I had no idea you could do that.... Quick google: seems you would print "ex.getMessage()". Is that what you meant?
10 years ago
If I declare a variable inside a catch try block, that variable is not available outside of the block. Example:



On the other hand, if I declare the variable outside the try block, I'm told the variable might not be initialized. Example:



I suppose I could initialize the variable to a temporary value that it's never really supposed to have, but that seems risky so I'm guessing there's a better way. Thoughts?
10 years ago
The error is as follows:


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

10 years ago
Code is as follows:



If I input the number "5" or "5,0", the program works. For some reason it doesn't work if I write "5.0", despite printing "5.0" itself if I write "5,0". What's up with this?
10 years ago
Hmm, I'm still confused as to how I should have known this, but thanks for clearing this up.

Thanks!
12 years ago
a.length returns the length of the array a, but how does it actually work and more importantly, how would I go about finding out? Normally this usage of the . operator would mean that length would be a method inside of an a object, right? Is that the case here? If so, where does the method come from? The api docs on array gives me a getLength method, but that seems like something else.

Can someone please explain this to me? :-) I understand how to use .length, but I find it frustrating not understanding what actually happens.

12 years ago

Jesper de Jong wrote:Welcome to the Ranch!

Thanks!

Ah yes, that makes sense. All right, now that I see it, it's actually pretty obvious. Thanks again.
12 years ago