Skip to content

Tag: java

Conserving RAM in java by using references to objects

I know some about java, and a lot about the lower level functions of a computer, so I’m always looking for ways to conserve ram. I’m also a fan of dwarf fortress, so I’ve been trying to get some similar system working. So, what I want to do is to make multiple tiles, assign them some type of…

How to reduce Optionals to throw an error once

I have the following response coming from a rest call and performing some logic based on what is returned. This is what I want. If the overall status code is NOT 200 OR If within the list of SimpleResponse, none of the SimpleResponse objects has a 200 httpCode, throw an error. The example below is incomplete.…

How to create a Path to a non existent file

How can I create a Path to a file which is guaranteed not to exist? I thought about using an “impossible” path name, such as: But somebody could create such a file, making my code fail. Update I have written a method which accepts a Path and then tries to read from the underlying file, and now I a…

Stack in Java not being able to print in specific order

Currently I’m working on a project that is supposed to be a reverse word guessing game. This is how it should work: The use keeps guessing the word until it is fully spelled out. If the user guesses the letter correctly it is revealed if not it is still reveled as shown below ⚠️ Currently I have a stack…

Skip folder if it does not exists using java

I have created a property file in java named prop.properties inside eclipse. In prop.properties file, I have added below lines:- users = user1,user2,user3 where user1 , user2 and user3 are folders. I have created directory named TestDir which contains, user1,user3 folders Suppose if user2 folder is not presen…

Java MultiThreading task, three smokers and one agent

the task consists of three smokers which are three threads and one class called Agent which has three attributes Tobacco, Lighters and Paper. Smokers only have one of these items with them, agent class is supposed to put two items randomly on the table and then Smoker is supposed to check if they are missing …

Deserialize empty JSON array into empty TreeMap

I’m new to Java, and I’m wondering how to deserialize an empty JSON array into an empty Java object of type TreeMap<String, MyOtherClass>. Currently, I’m attempting to deserialize a JSON file with an array of objects, and each object into a class called MyClass. The class is roughly as…

How to convert Date string into UTC Date object?

I’m learning Java and come across this issue. I have a date string with the given format. Now I wanted to initialize/create a Date object of UTC timezone. For this, I have tried below code This will convert the time into UTC instead of just creating a date object. Now I’m confused as to which is t…