Java
ArithmeticException: “Non-terminating decimal expansion; no exact representable decimal result”
From the Java 11 BigDecimal docs: When a MathContext object is supplied with a precision setting of 0 (for example, MathContext.UNLIMITED), arithmetic operations are exact, as are the arithmetic methods which take no MathContext object. (This is the only behavior that was supported in releases prior to 5.) As a corollary of computing the exact result, the rounding mode setting of a MathContext object with … Read more
How to convert a date to milliseconds
You don’t have a Date, you have a String representation of a date. You should convert the String into a Date and then obtain the milliseconds. To convert a String into a Date and vice versa you should use SimpleDateFormat class. Here’s an example of what you want/need to do (assuming time zone is not involved here): Still, be careful because in Java the milliseconds obtained are the … Read more
Java Fraction Calculator
When you class mates tell you, that you didn’t call the reduce method, they mean, that you never use the reduce method. Your add-method should look somewhat like this: and the multiply method should look like this: Remember that you also have to change your reduce method, as it always returns 1 right now! Edit: Added code to print fraction … Read more
Exception in thread “main” java.net.BindException: Address already in use – Error in Netbeans only
The way to achieve what I want is to right-click on the particular class (ex. Server.java) that I want to run and select “Run this file”. This enables me to run only the Server app. Then, do the same process for the other file, Client.java. However, Netbeans is somewhat confusing/deceiving in this particular circumstance. What Netbeans does … Read more
Netbeans – Error: Could not find or load main class
Right click on your Project in the project explorer Click on properties Click on Run Make sure your Main Class is the one you want to be the entry point. (Make sure to use the fully qualified name i.e. mypackage.MyClass) Click OK. Clean an build your project Run Project 🙂 If you just want to … Read more
Is there a way to end the program if statement is false?
This should end it!
Unable to resolve host “” No address associated with host name
You probably don’t have the INTERNET permission. Try adding this to your AndroidManifest.xml file, right before </manifest>: Note: the above doesn’t have to be right before the </manifest> tag, but that is a good / correct place to put it. Note: if this answer doesn’t help in your case, read the other answers!
What’s the difference between HashSet and Set?
A Set represents a generic “set of values”. A TreeSet is a set where the elements are sorted (and thus ordered), a HashSet is a set where the elements are not sorted or ordered. A HashSet is typically a lot faster than a TreeSet. A TreeSet is typically implemented as a red-black tree (See http://en.wikipedia.org/wiki/Red-black_tree – I’ve not validated the actual implementation of sun/oracle’s TreeSet), whereas a HashSet uses Object.hashCode() to create an index in … Read more
Can an int be null in Java?
int can’t be null, but Integer can. You need to be careful when unboxing null Integers since this can cause a lot of confusion and head scratching! e.g. this: will give you a NullPointerException, despite object not being null! To follow up on your question, if you want to indicate the absence of a value, I would investigate java.util.Optional<Integer>