Jump to content

Withdraw items in an array


Recommended Posts

Posted (edited)

I am trying to create an array and if items in that array exist, withdraw them from the bank.

 

If I create the array:

String[] items = new String[] {"Coins", "Death rune"};

 

How do I withdraw any items that exist in the array from the bank?

 

if (bank.isOpen()){

bank.withdrawAll(items);

}

 

this is the wrong answer ;)  Anyone help me with the correct answer?

 

 

 

 

 

Edited by sudoinit6
Posted
9 minutes ago, sudoinit6 said:

I am trying to create an array and if items in that array exist, withdraw them from the bank.

 

If I create the array:

String[] items = new String[] {"Coins", "Death rune"};

 

How do I withdraw any items that exist in the array from the bank?

 

if (bank.isOpen()){

bank.withdrawAll(items);

}

 

this is the wrong answer ;)  Anyone help me with the correct answer?

 

 

 

 

 

Use a HasMap: https://osbot.org/api/org/osbot/rs07/api/Bank.html#withdraw-java.util.HashMap-

Pretty sure if you set the amount > the amount you have it will withdraw all.

  • Like 1
Posted
7 minutes ago, Acerd said:

why not just do withdrawAll("Coins", "Death rune");

Because the actual list is going to be longer.

 

6 minutes ago, The Undefeated said:

String[] items = new String[] {"Coins", "Death rune"};
for (String i : items) {
    if (getBank().contains(i) {
      getBank().withdrawAll(i);
    }
}
      

 

Danke`!

 

  • 1 year later...
Posted (edited)
On 5/11/2017 at 10:12 PM, The Undefeated said:

String[] items = new String[] {"Coins", "Death rune"};
for (String i : items) {
    if (getBank().contains(i) {
      getBank().withdrawAll(i);
    }
}
      

 

Using this method, you're unable to single out an item from the array. You'll just withdraw everything in the array. Unless that is what you want, a method to pick out singular items from an array would be more useful surely?

Would this work?

String[] items = new String[] {"Coins", "Death rune"};
for (String i : items) {
    if (getBank().contains(i) {
      getBank().withdrawAll(i[0]); //For coins
    }
}

 

Edited by Glaciation96
Posted (edited)
33 minutes ago, Glaciation96 said:

Using this method, you're unable to single out an item from the array. You'll just withdraw everything in the array. Unless that is what you want, a method to pick out singular items from an array would be more useful surely?

Would this work?


String[] items = new String[] {"Coins", "Death rune"};
for (String i : items) {
    if (getBank().contains(i) {
      getBank().withdrawAll(i[0]); //For coins
    }
}

 

 

This post is a year old, not sure why you gravedigged it.

Your code doesn't really make sense either (the whole point is to withdraw every item in the array, not a single one)

 

 

Edited by Explv
Posted
20 hours ago, Explv said:

 

This post is a year old, not sure why you gravedigged it.

Your code doesn't really make sense either (the whole point is to withdraw every item in the array, not a single one)

 

 

I'm not intentionally looking for old posts, just digging through all the resources that I can find, then commenting to test my own understanding of java/botting lol. Also does my code not make sense or not work? So long as it's not the latter I'm happy ?

 

He also asked for "any items" which I guess could translate to all or just some of the items, to me anyway. Nice Explv's Map btw, been using that loads. 

Posted
5 minutes ago, Glaciation96 said:

I'm not intentionally looking for old posts, just digging through all the resources that I can find, then commenting to test my own understanding of java/botting lol. Also does my code not make sense or not work? So long as it's not the latter I'm happy ?

 

He also asked for "any items" which I guess could translate to all or just some of the items, to me anyway. Nice Explv's Map btw, been using that loads. 

no it doesn't make any sense lol.

Posted (edited)
1 hour ago, Glaciation96 said:

I'm not intentionally looking for old posts, just digging through all the resources that I can find, then commenting to test my own understanding of java/botting lol. Also does my code not make sense or not work? So long as it's not the latter I'm happy ?

 

He also asked for "any items" which I guess could translate to all or just some of the items, to me anyway. Nice Explv's Map btw, been using that loads. 

Well both really, there's a syntax error 

getBank().withdrawAll(i[0]); //For coins

I assume that should be items[0] not i[0]

But the code doesn't really make sense, if the bank contains "Death rune"s and not "Coins" you would still try to withdraw Coins. And you would never withdraw Death runes.

If you wanted to withdraw any item from an array of items you could add a break after the withdraw in undefeated's code, or do something like:

Arrays.stream(items)
        .filter(itemName -> getBank().contains(itemName))
        .findAny()
        .ifPresent(itemName -> getBank().withdrawAll(itemName));
Edited by Explv
  • Like 2
Posted (edited)
3 hours ago, Explv said:

Well both really, there's a syntax error 


getBank().withdrawAll(i[0]); //For coins

I assume that should be items[0] not i[0]

But the code doesn't really make sense, if the bank contains "Death rune"s and not "Coins" you would still try to withdraw Coins. And you would never withdraw Death runes.

If you wanted to withdraw any item from an array of items you could add a break after the withdraw in undefeated's code, or do something like:


Arrays.stream(items)
        .filter(itemName -> getBank().contains(itemName))
        .findAny()
        .ifPresent(itemName -> getBank().withdrawAll(itemName));

Many thanks for the clarification! Gravedigging this thread turned out very beneficial to my understanding in the end ?

EDIT: For the Array Stream, I'll have to come back to that in a few weeks time lol, but it's great to have it there for later reference.

Edited by Glaciation96

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...