April 8, 20187 yr OK, I may be in over my head here but here is what I am trying to do: Loop though the GE boxes and if one is pending sale loop through the array goods and if it matches get the price it is currently offered for and set the static variable for the price of that good to one less than it is offered for now. For now the only good in the array is Law Rune. So the static in my main lass is lawRuneSell. This code creates the string j+"Sell" which is Law RuneSell, strips the whiite space and removes the space so my string ssl is eventuall lawRuneSell which matches my static string. But I cant set lawRuneSell to ssp because one is a String and one is an int. How can I use ssl to set lawRuneSell? Spoiler for ( String j : goods){ script.grandExchange.getItemPrice(i); RS2Widget fs = script.widgets.getWidgetContainingText(j); if (fs != null){ if (fs.isVisible()){ String ss = new String(j + "Sell"); ss.replaceAll("\\ss",""); String ssl = new String(Character.toLowerCase(ss.charAt(0)) + ss.substring(1)); int ssp = script.grandExchange.getItemPrice(i) - 1; ssl = ssp; fs.interact("View offer"); } } }
April 8, 20187 yr So you're trying to set a variable based on its name, and you're constructing the name by concatenating strings? This is probably a sign there's that there's room for a better solution, mostly because to my knowledge there's no way to meta-interact with java in this way, not least because variable names and the like are not replicated in the compiled byte-code. Instead, you might find the map data structure useful. Take a look: https://docs.oracle.com/javase/8/docs/api/?java/util/Map.html GL Apa
April 8, 20187 yr Author 31 minutes ago, Apaec said: So you're trying to set a variable based on its name, and you're constructing the name by concatenating strings? This is probably a sign there's that there's room for a better solution, mostly because to my knowledge there's no way to meta-interact with java in this way, not least because variable names and the like are not replicated in the compiled byte-code. Instead, you might find the map data structure useful. Take a look: https://docs.oracle.com/javase/8/docs/api/?java/util/Map.html GL Apa Thanks Apaec, I am sure there is a better way. Hopefully maps are it. Watching some tutorials now.
April 8, 20187 yr 1 hour ago, sudoinit6 said: Thanks Apaec, I am sure there is a better way. Hopefully maps are it. Watching some tutorials now. Glad to hear Don't hesitate PM me if you run into any issues or have any questions Apa
April 8, 20187 yr Author So the maps stuff is going to take me some time to wrap my head around. What I did find that I think will work (although it is inelegant) is this: Made a new class that I pass i and the int to and it sets the static: Spoiler public class SetSellPrice { public void getGood (String x, int y){ if(x == "Law rune"){ Task.lawRuneSell = y;} } } Not sure if it works yet, but Eclipse accepts it so that's a good sign, I will just if else through the array. I understand that this is not very scalable, but I am not dealing with a large number of goods so until I can do better this will have to do.
April 8, 20187 yr 20 minutes ago, sudoinit6 said: So the maps stuff is going to take me some time to wrap my head around. What I did find that I think will work (although it is inelegant) is this: Made a new class that I pass i and the int to and it sets the static: Reveal hidden contents public class SetSellPrice { public void getGood (String x, int y){ if(x == "Law rune"){ Task.lawRuneSell = y;} } } Not sure if it works yet, but Eclipse accepts it so that's a good sign, I will just if else through the array. I understand that this is not very scalable, but I am not dealing with a large number of goods so until I can do better this will have to do. I'd suggest taking a look at Lists first if you haven't seen them before, it'll make understanding Maps a lot easier.
April 11, 20187 yr On 4/9/2018 at 3:09 AM, sudoinit6 said: OK, I may be in over my head here but here is what I am trying to do: Loop though the GE boxes and if one is pending sale loop through the array goods and if it matches get the price it is currently offered for and set the static variable for the price of that good to one less than it is offered for now. For now the only good in the array is Law Rune. So the static in my main lass is lawRuneSell. This code creates the string j+"Sell" which is Law RuneSell, strips the whiite space and removes the space so my string ssl is eventuall lawRuneSell which matches my static string. But I cant set lawRuneSell to ssp because one is a String and one is an int. How can I use ssl to set lawRuneSell? Hide contents for ( String j : goods){ script.grandExchange.getItemPrice(i); RS2Widget fs = script.widgets.getWidgetContainingText(j); if (fs != null){ if (fs.isVisible()){ String ss = new String(j + "Sell"); ss.replaceAll("\\ss",""); String ssl = new String(Character.toLowerCase(ss.charAt(0)) + ss.substring(1)); int ssp = script.grandExchange.getItemPrice(i) - 1; ssl = ssp; fs.interact("View offer"); } } } Maps are definitely going to help you, or even an enum if you want to store more information about it (such as unnoted ID & noted ID) If you can work off of IDs rather than item name then this lil snippet might help you also GrandExchange.Box[] getBoxesWithID(int id) { return Arrays.stream(GrandExchange.Box.values()).filter(box -> api.grandExchange.getItemId(box) == id) .toArray(s -> new GrandExchange.Box[s]); } Edited April 11, 20187 yr by Slut
April 11, 20187 yr On 09/04/2018 at 12:04 AM, sudoinit6 said: So the maps stuff is going to take me some time to wrap my head around. What I did find that I think will work (although it is inelegant) is this: Made a new class that I pass i and the int to and it sets the static: Hide contents public class SetSellPrice { public void getGood (String x, int y){ if(x == "Law rune"){ Task.lawRuneSell = y;} } } Not sure if it works yet, but Eclipse accepts it so that's a good sign, I will just if else through the array. I understand that this is not very scalable, but I am not dealing with a large number of goods so until I can do better this will have to do. Generally, using static variables to communicate between classes is frowned upon as it goes against object oriented principles. It may work, but as you get more and more confident with java it might be worth moving away from this concept and trying to learn more about OO. Also, i'd avoid using strings to represent data, but now i'm just getting picky! Keep practising Apa Edited April 11, 20187 yr by Apaec
April 15, 20187 yr Author On 4/11/2018 at 6:39 AM, Apaec said: Generally, using static variables to communicate between classes is frowned upon as it goes against object oriented principles. It may work, but as you get more and more confident with java it might be worth moving away from this concept and trying to learn more about OO. Also, i'd avoid using strings to represent data, but now i'm just getting picky! Keep practising Apa I appreciate the advice and get that using statics is bad practice, I will use it as a crutch to get me by until I really get my head wrapped around maps. That may be awhile because this is just a part time hobby for me but it's been a fun road so far. Thanks so much for your help. Thanks to you as well Slut.
April 15, 20187 yr Author On 4/8/2018 at 5:26 PM, d0zza said: I'd suggest taking a look at Lists first if you haven't seen them before, it'll make understanding Maps a lot easier. So I ended up using a ArrayList for the script I am currently working on and it does the trick, aside from the above mentioned bad habits. Thanks for that tip!
Create an account or sign in to comment