MarWo22 Posted June 8, 2018 Posted June 8, 2018 I'm using this snippet for getting the prices of items from Osbot Spoiler public class GrandExchange { private static final String BASE = "https://api.rsbuddy.com/grandExchange?a=guidePrice&i="; /** * Default Constructor * */ public GrandExchange() { } /** * Gets the overall price of an item. * * @param itemID * @return itemPrice * @throws IOException */ public static int getOverallPrice(final int itemID) throws IOException { return parse(itemID,"overall"); } /** * Gets the buying price of an item. * * @param itemID * @return itemPrice * @throws IOException */ public static int getBuyingPrice(final int itemID) throws IOException { return parse(itemID,"buying"); } public static int getSellingPrice(final int itemID) throws IOException { return parse(itemID,"selling"); } /** * Retrieves the price of an item. * * @param itemID * @return itemPrice * @throws IOException */ private static int parse(final int itemID, String choice) throws IOException { final URL url = new URL(BASE + itemID); BufferedReader file = new BufferedReader(new InputStreamReader(url.openStream())); String line; String price = null; while ((line = file.readLine()) != null) { if (line.contains("{")) { price = (line).trim(); } } if (choice.equals("buying")){ price = price.substring(price.indexOf(",") + 10, nthOccurrence(price, ',', 1)).trim(); } else if(choice.equals("selling")) { price = price.substring(nthOccurrence(price, ',', 2) + 11 , price.indexOf("sellingQuantity") - 2).trim(); } else { price = price.substring(price.indexOf(":") + 1, price.indexOf(",")).trim(); } file.close(); return Integer.parseInt(price); } private static int nthOccurrence(String str, char c, int n) { int pos = str.indexOf(c, 0); while (n-- > 0 && pos != -1) pos = str.indexOf(c, pos + 1); return pos; } } Im trying to get the best margin of making the potions. So i came up with this. But because of the IOexception in the snippet it doesnt work. Can someone please help me how to fix this? Im still a beginner at scripting and java. Spoiler public enum Potion { IRIT(259, 101), TOADFLAX(2998, 3002), AVENTOE(261, 103), KWUARM(263, 105), CANDENTINE(265, 107), LANTADYME(2481, 2483); public final int HerbPrice; public final int Margin; public final int UnfinishedPrice; public final String Name; public final int HerbId; public final int UnfinishedId; private final String UnfinishedName; Potion(int herbId, int unfinishedId) throws IOException { Name = name().substring(0,1).toUpperCase() + name().substring(1).toLowerCase(); UnfinishedName = Name + " potion (unf)"; HerbId = herbId; UnfinishedId = unfinishedId; HerbPrice = Math.max(GrandExchange.getSellingPrice(herbId), GrandExchange.getBuyingPrice(herbId)); UnfinishedPrice = Math.min(GrandExchange.getSellingPrice(unfinishedId), GrandExchange.getBuyingPrice(UnfinishedId)); Margin = UnfinishedPrice - HerbPrice; } }
Canidae Posted June 9, 2018 Posted June 9, 2018 (edited) Please copy the NPE you got and note which line it came from. Edited June 9, 2018 by Canidae