distraction 61 Share Posted July 6, 2017 Someone asked about this in discord earlier today, and I helped them get it working. I figured I would post it here in case anyone else has the same issue. Basically, he needed to get the % value from the string "Wintertodt's Energy: 35%". To do this, I used a regular expression to grab only the numbers from the string, and then used `parseInt` on the result. Here is an example on how to do this. // this should be replaced with whatever code fetches the string String str = "Wintertodt's Energy: 35%"; // create the regex pattern to find numbers Pattern ints = Pattern.compile("\\d+"); // run the regex pattern on the string we want to parse the numbers from Matcher makeMatch = ints.matcher(str); makeMatch.find(); // set the result String result = makeMatch.group(); // this should print 35 in your console (note that 35 is actually still a string at this point) log(result); // parse the number from the result string to convert it to the right data type (int) int integerValue = Integer.parseInt(result); // this should print 35 in your console (note that 35 is an int here, which is why we have to do integerValue+"" to get it to log since log takes in a string) log(integerValue+""); Once you have `integerValue`, you can do things like `if (integerValue > 50)` or `if (integerValue == 100)`. Let me know if you have any questions or suggestions on an easier way to do this. Here are the required imports for the code above import java.util.regex.Matcher; import java.util.regex.Pattern; Link to comment Share on other sites More sharing options...
Fictional 1 Share Posted July 7, 2017 Thanks a lot distraction. This helped me out tremendously Link to comment Share on other sites More sharing options...
distraction 61 Author Share Posted July 8, 2017 Thanks a lot distraction. This helped me out tremendously No problem man, I'm glad I was able to help Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.