Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

Ben Ernstberger

Ranch Hand
+ Follow
since Aug 17, 2018
Ben likes ...
Android Netbeans IDE Java
Munich, Germany
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ben Ernstberger

Does somebody know a free Java server where I can put a Java server application? Thanks for help.
You can't put a catch clause that catches a Superclass above a catch clause that catches a subclass. So in this example the catched exception can't be a Superclass of Runtime exception. StackOverflowError is a subclass of throwable but it isn't in the same branch of the tree as Runtime exception because it is an error. So it is allowed because StackOverflowError isn't a Superclass of Runtime exception.
Thank you very much. That helped me a lot.
3 years ago
I do not think that's a good idea. Instead, I would write a class bank that stores all accounts and their transactions.
3 years ago

I want to pack small rectangles in a big rectangle which should be as small as possible. The small rectangles are fixed by length and width and can't rotate. I search for an algorithm which solves that problem as fast as possible.

Thank you
3 years ago
private static String[][] transactions=new String[6][number];

Here is the mistake. At this point number has the value 0. So the size of your array is always [6][0] because the size of an array is defined at the time of object creation and can't be changed later. If you want an array with variable size you should use ArrayList.
3 years ago
The equals method would only return true if s1 and s2 would reference the same object. But that's not possible because s1 is an object of class String and s2 an object of class StringBuilder. That are different classes and therefore different objects.
To your second question:

why is the given answer (Answers: D, E, F) correct?

The code compiles and in the for block the output will be produced:
If getChickens returns an ArrayList containing one or more chickens for each chicken the output "cluck" will be produced (answers d and e).
If getChickens returns null or an ArrayList containing zero elements the code will crash with a NullPointerException or an IndexOutOfBoundsException (answer f).
To your first question:

alex chalyy wrote:
Is there a mistake in the book?

I don't think so because that is a blank line and you have to complete the semicolon in your mind like the rest of the line.

The code you put in your copyOf method looks good and I think it's the best way to create a deep copy of your object.
But normally there is used the copy method, which is inherited from object, to copy an object. So you should override that method with the code of your copyOf method and remove your old method. Also there is no need to marke the method as static. If you do that your class have to implement the marker-interface Cloneable.
3 years ago
I think the InputMismatchException is thrown by the nextInt method because there is no next int in the InputStream.
3 years ago
I'm from Germany and I have that problem too. I solve the problem by reading English books about Java and by discussing in English about Java like here on JavaRanch. Granted I still lose a lot of time by understanding questions but I think to master Java completely you have also to master the English language because the most in Java is in English. For Example the API is only in English available.

But if you practice a lot I'm sure you became faster in understanding questions. Good luck!
That is the raw type and it should never used in new code. It is only used in old code that has no generic types.
StringBuilder uses characters to manage the String and only builds a String if you invoke toString(). So the answer to that question is: To use a StringBuilder is much more efficient than using Strings.
How it looks like with Arrays and ArrayLists I don't know.
3 years ago

Malik Ashter Mehdy wrote:And if I remove diamond operator on the right side error goes away.

You should never do that because that is the raw type. If the diamond operator doesn't work, you should use the type also on the right side.