user1905315
user1905315

Reputation: 55

Java explode by space

I have a function

if (text.startsWith("item")) {
            int x = 1;
            int y = 1;
            int id = Integer.parseInt(text.substring(7));
            int amount = ???
            Stream.createFrame(x);
            Stream.writeDWord(y);
            Stream.writeDWord(id); //itemid
            Stream.writeDWord(10000); //amount
}

it should add an item to inventory, id can be 1 to 6 characters long, i need to explode that amount integer from string, how do i do that in this case? I hope you guys understand what i am asking for, i am a caveman in java..

Upvotes: 0

Views: 144

Answers (2)

user902383
user902383

Reputation: 8640

you can do something like that

String[] array = test.split("\\s+");
if (array.length != 3)
{
//report invalid data format
}
else
{
int id = Integer.parseInt(array[1]);
int amount = Integer.parseInt(array[2]);
}

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691913

String testString = "item 456 234";
String[] elements = testString.split(" ");
int id = Integer.parseInt(elements[1]);
int amount = Integer.parseInt(elements[2]);

Upvotes: 2

Related Questions