Jump to content

JAVA 17: How extract a set of data from an object.

HydrogenOS

Say I have a string called "pos" and it contains:

Location{world=CraftWorld{name=world},x=10.0,y=127.0,z=-30.0,pitch=0.0,yaw=0.0}

How would I extract the x value?

NOTE: the value of x changes each time it is used

Link to comment
Share on other sites

Link to post
Share on other sites

I would use regEx... In this case "x=(?<x>[^,]*),"

This creates a named capture group "x" and extracts the value after x= and before the following comma.

Link to comment
Share on other sites

Link to post
Share on other sites

You can always split this string by its commas into array of strings and iterate over the list. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Where does this string come from? From what I can see "CraftWorld{name=world}" is the output of a CraftWorld instance's toString() method. Can you maybe share a little more context?

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Maybe not the answer you're looking for, but I wouldn't try to use that string as is. I'd want it to be output in a more usable, defined format, like JSON or CBOR. Something more like 

{"world":{"name":"name"},"x":10.0,"y":127.0,"z":-30.0,"pitch":0.0,"yaw":0.0}

With that, it's easy to use something like Gson to encode/decode it into your object.

 

Personally, as a Kotlin programmer, I like Kotlinx Serialization where implementing this becomes trivial:

@Serializable
data class Location(
    val world: CraftWorld,
    val x: Double,
    val y: Double,
    val z: Double,
    val pitch: Double,
    val yaw: Double,
) {
    @Serializable
    data class CraftWorld(val name: String)
}

fun main() {
    val pos = """{"world":{"name":"name"},"x":10.0,"y":127.0,"z":-30.0,"pitch":0.0,"yaw":0.0}"""
    val obj = Json.decodeFromString<Location>(pos)
    println(obj)
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 1/13/2024 at 1:51 PM, QuantumRand said:

I'd want it to be output in a more usable, defined format, like JSON or CBOR.

That assumes OP has any control over how the string is created. Right now, my assumption would be it's the output of toString(). But we don't know if OP is calling that method or something else is.

 

Creating it in a better format would effectively require them to have access to an instance already. Which would mean they could skip using a string and simply use the instance directly. Which is why I asked for more context, since we don't really know where that string is coming from or what they intend to do with the value.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×