Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!

Zachary Griggs

Rancher
+ Follow
since Apr 29, 2016
Cows and Likes
Cows
Total received
15
In last 30 days
0
Total given
0
Likes
Total received
71
Received in last 30 days
4
Total given
167
Given in last 30 days
15
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Zachary Griggs

I ran this, and got false - true - true.

This returns true:

And this:

Returns false - false - false.

Something to do with the Java String Pool internals and which Strings are interned by default, I'd guess.

I wouldn't quite say this is a good interview question - detailed knowledge of the Java String Pool internals are probably not too relevant to real career work.
1 day ago
Ok, so I actually did this sooner than I thought I would

If anyone else is having the same problem as me, here is the full solution I made. This will create a file called RES-INDEX in the resources directory which stores paths to all other resources. It uses this file when trying to discover resources to load. Now I can use my code in jar as well as in IntelliJ.







Then you make your code call ResourceIndexer on the pre-package step of Maven Package:
1 day ago
Try this on a Terminal window

If you need the development kit:

Verify:


First command ensures you have all the up-to-date available applications to download
Second command uses apt to install the runtime environment
Third command uses apt to install the development kit

Basically apt is a utility to install/remove applications.

JavaFX may be more work.. I think you need some other downloads for it, but I've never ran it on Linux.
3 days ago
Interesting - those both sound like great ideas. Thanks!

I think for now I am just going to have the files not be resources, but rather just go in the root directory of the application. They are just game configuration/graphics files so there's nothing sensitive. When/if I release it, I figure I will look into one of those solutions.

When I get to that point, I'll probably post the solution here if I make one.
3 days ago
I see. I also have an important directory structure under the resources folder and use the methods in File to find all files under that directory. I guess this isn't something that can be done with an input stream - so is this type of use case just unsupported then?  I can always just find a different way, I guess there's no reason I can't just put the resources folder in the root directory.
4 days ago
I have a Java application that uses a lot of resource files. Running this application in IntelliJ works fine, and finds the resources correctly using this helper:

When I package this project into a jar file and try to run it, it cannot find any of the resources. Sample error message:

(File 'file:\C:\Users\...\...-1.0-SNAPSHOT.jar!\engine_extensions\definitions' does not exist)



All my resources are in the default directory (src/main/resources) and I use Maven to package it.

Am I missing something here, or do resources simply not work for a jar file? And if this is the case, how would you recommend managing resource files like this that I need for internal use?
5 days ago
Every line of code runs in order. Therefore, the code enters the first (outer) loop, and finds another loop. It will now run the entire second (inner) loop to completion until it proceeds to the next line, where it continues with the outer loop execution.

So, for the first iteration of the outer loop, i == 0.
Now it runs inner loop. The first iteration, j == 0. It runs code for [0,0].
Now second iteration for inner loop, j == 1. It runs code for [0,1].
Continuing, j == 2, runs code for [0,2]
Exits inner loop, because j < 3 won't be true anymore.

Then it proceeds to the second iteration of the outer loop, i == 1.
You can probably see how it goes from here.
5 days ago
You are using improper String comparisons.

Strings are an object type, which is different from a native type like an int. For any Object type, the "==" operator compares the address of the object, not the contents of the object. This matters, because when you compare gender to "m", you are creating a new String which contains the letter "m", and comparing it to an existing String which contains the letter "m". They are not equal, since they don't have the same address.

You have a comment in your code on your line that assigns gender that tells you the way to fix this. Specifically, you need to compare the Strings in a way that compares the contents instead of the address (you can use .equalsIgnoreCase)

Also a general comment - all those variables you use should be private. You also don't need to load the actual image until the user needs to see that image - though this is more of an efficiency concern that won't matter for such a small application.
6 days ago
Hopefully it's just being done as a learning exercise - it's a popular one since recursive Fibonacci number is a fun one to write.

If you actually need the number, you can even do it in constant time using Binet's formula (approximation but very accurate with more decimal places).
1 week ago
In order to understand it, you need to understand what System.in is. System.in is a static input stream variable. It does not get instantiated by the developer, and there's only one instance of it that ever exists. Therefore when you create two Scanners pointing at System.in, they are both pointing to the same input stream.

So when you close that one input stream, both Scanners that use the input stream can no longer process data from it.

It is not documented in Scanner - and it should not be, because it's not a behavior of Scanner. It's a behavior of System.in. If you use System.in in any other class that uses an Input Stream and you close it, it'll do the same thing.
1 week ago
Use PreparedStatements - don't write your own logic.

Create a PreparedStatement with a question mark where you want to insert your parameter:


Insert the parameter into the statement:
2 weeks ago
To answer the part I know:

You can create a new Thread for your database access code, if you want. To do this, define a new Thread, and in the argument of the constructor, create a new (or use an existing) Runnable. Runnable is an interface which defines a simple run() method, so you can simply make an anonymous runnable. Make sure to call thread.start(), instead of thread.run()!

When you want to make updates to the UI from the new Thread you made, make sure to call Platform.runLater() or it won't let you do it. No threads besides the UI thread are allowed to update the UI, so Platform.runLater schedules a UI update on the UI thread.

So you can think of the process like this:
- UI element has an event triggered on it (like a button press)
- UI element schedules and launches a new Thread
- UI thread continues unblocked
- Database thread accesses MongoDB
- Database thread schedules a UI modification on the UI thread
- UI thread performs desired modification

POJO are plain old java objects. Essentially it's an object with no real custom behavior, it just has private fields and public getters/setters and the equals/hashcode method implemented. The POJO should be the model object that you want to store in your database. In AWS (which I have experience with) we convert those into a data transfer object which specifies the hash key and sort key, I imagine the DAO for mongodb is similar.
2 weeks ago
The names dx and dy implies that those variables should be the change in x and change in y, not the new x and y values. For example is x is 100, and dx is 10, then the new x value would be 110. How would you do this in code, what change do you need to make?

Also, you make int x and int y as local variables. This means that when the method ends, those x and y values will no longer exist - they are local to the method. Instead, you need to save it in a class level variable so you can use it later. What class-level variables store the x and y location of the rectangle?
3 weeks ago
Interestingly, it looks like as of Java 8, that Math.random() is actually just a wrapper call to Random.nextDouble() on a singleton instance of a Random:




Very strange.
3 weeks ago