March 28, 201510 yr RSBuddy/OSBuddy Exchange has more accurate prices than Zybez for the most part. import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class PriceLookup { private static URLConnection con; private static InputStream is; private static InputStreamReader isr; private static BufferedReader br; private static String[] getData(int itemID) { try { URL url = new URL( "https://api.rsbuddy.com/grandExchange?a=guidePrice&i=" + itemID); con = url.openConnection(); is = con.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); String line = br.readLine(); if (line != null) { return line.split(","); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } else if (isr != null) { isr.close(); } else if (is != null) { is.close(); } } catch (Exception e) { e.printStackTrace(); } } return null; } public static int getPrice(int itemID) { String[] data = getData(itemID); if (data != null && data.length == 3) { return Integer.parseInt(data[0].replaceAll("\\D", "")); } return 0; } public static int getAverageBuyOffer(int itemID) { String[] data = getData(itemID); if (data != null && data.length == 3) { return Integer.parseInt(data[1].replaceAll("\\D", "")); } return 0; } public static int getAverageSellOffer(int itemID) { String[] data = getData(itemID); if (data != null && data.length == 3) { return Integer.parseInt(data[2].replaceAll("\\D", "")); } return 0; } } Dump of all the prices from March 27, 2015: http://pastebin.com/rQ5dk7mp Edited March 28, 201510 yr by VladBots
March 28, 201510 yr nice. close your streams and readers :| Botre never satisfied Closing streams is programming 101. If you dont close streams the buffer might not get flushed and you can get unexpected results. Edited March 28, 201510 yr by Mysteryy
March 28, 201510 yr Closing streams is programming 101. If you dont close streams the buffer might not get flushed and you can get unexpected results. Oh...Im in my second semester of taking AP Comp Sci. Right now were working on inheritance
March 28, 201510 yr Oh...Im in my second semester of taking AP Comp Sci. Right now were working on inheritance Are you using Big java for your book? If you are, all the test are based off of a teacher copy of the book. I probably still have mine if you'd want to see it Edited March 28, 201510 yr by twin 763
April 23, 201510 yr The getPrice(), getAverageBuyOffer(), and getAverageSellOffer() methods are incorrect. String[] data will always be length 5, not length 3, so those methods will always return 0. For a simple fix, you may be able to change it to data.length==5 or just delete that parameter overall. Also, it's fairly simple to split each index of the array by the ":" because the second half will be the price you want and the first half will be the description. For example, if you go to the link for iron ore (https://api.rsbuddy.com/grandExchange?a=guidePrice&i=440), you can see that the stream is: {"overall":54,"buying":54,"buyingQuantity":125855,"selling":53,"sellingQuantity":103603} Split up in the getData() method, It looks like this String[] data = { ""overall":54", ""buying":54", ""buyingQuantity":125855", ""selling":53", ""sellingQuantity":103603" } If you split by colon, data[0].split(":") returns the array {""overall"", 54}. then, returning the parse of index 1 gives you 54. Furthermore, the getter methods would return the wrong values, because data[2] is actually the buying quantity, not the selling price. It is important to change the return statement of getAverageSellOffer() to use data[3]. Here are the changes I made: public static int getPrice(int itemID) { String[] data = getData(itemID); if (data != null && data.length == 5) { return Integer.parseInt(data[0].split(":")[1]); } return 0; } public static int getAverageBuyOffer(int itemID) { String[] data = getData(itemID); if (data != null && data.length == 5) { return Integer.parseInt(data[1].split(":")[1]); } return 0; } public static int getAverageSellOffer(int itemID) { String[] data = getData(itemID); if (data != null && data.length == 5) { return Integer.parseInt(data[3].split(":")[1]); } return 0; } Edited April 23, 201510 yr by bthebrave
April 27, 201510 yr -snip- If you're still using this you should transfer over to the new Grand Exchange API itself by Alek. Edited April 27, 201510 yr by 7331337
Create an account or sign in to comment