This site requires JavaScript, please enable it in your browser!
Greenfoot back
Proprogrammer04
Proprogrammer04 wrote ...

2019/2/19

Get a part out of a String

Proprogrammer04 Proprogrammer04

2019/2/19

#
Hi there, In my program, I want to use an ID System. It is going to sent to other classes as an String. (For example: An Oject of class test has the ID "1.3.5") No I want to get special parts of the ID but not the whole one. Has someone an idea how i can manage this? (I want to use this, so i won't have to send 3 different ints) Thank you in Advance ;-)
danpost danpost

2019/2/19

#
Proprogrammer04 wrote...
In my program, I want to use an ID System. It is going to sent to other classes as an String. (For example: An Oject of class test has the ID "1.3.5") No I want to get special parts of the ID but not the whole one. Has someone an idea how i can manage this? (I want to use this, so i won't have to send 3 different ints)
String partID = (testActor.getID().split("."))[0];
will set partID to "3" if the ID was "3.1.5". Change the index (0) to 1 or 2 for the other parts. As an alternattive, you can add a method to the test class or a class it is extended from:
public String getIdPart(int partNum)
{
    return (id.split("."))[partNum];
}
and get a part by using:
String partID = testActor.getIdPart(0);
Proprogrammer04 Proprogrammer04

2019/2/21

#
Thank you very much, I think that will solve the Problem ;-)
Proprogrammer04 Proprogrammer04

2019/2/21

#
There's only one Thing, I don't understand… At
String partID = (testActor.getID().split("."));
, what is getID() for? Is it an function or method in the oter class?
danpost danpost

2019/2/21

#
Proprogrammer04 wrote...
There's only one Thing, I don't understand… At
String partID = (testActor.getID().split("."))[0];
, what is getID() for? Is it an function or method in the oter class?
You said you only wanted a part of the id instead of the whole thing. I guess I presumed you had a getter method in the test class that returned the whole thing, like this:
public String getID()
{
    return id;
}
Proprogrammer04 Proprogrammer04

2019/2/22

#
Yes, i said that, thank you ;-)
You need to login to post a reply.