How to find a specific value after a specific word in Java?


I got a text from a BufferedReader and I need to get a specific value in a specific string.

This is the text:

aimtolerance = 1024; model = Araarrow; name = Bow and Arrows; range = 450; reloadtime = 3; soundhitclass = arrow; type = Ballistic; waterexplosionclass = small water explosion; weaponvelocity = 750; default = 213; fort = 0.25; factory = 0.25; stalwart = 0.25; mechanical = 0.5; naval = 0.5;

I need to get the exact number between <strong>default =</strong> and <strong>;</strong>

Which is "213"


Something like this....

String line; while ((line = reader.readLine())!=null) { int ind = line.indexOf("default ="); if (ind >= 0) { String yourValue = line.substring(ind+"default =".length(), line.length()-1).trim(); // -1 to remove de ";" ............ } }


If just care about the end result, i.e. getting stuff out of your '=' separated values text file, you might find the built in Properties object helpful?

<a href="http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html</a>

This does much of what you need. Of course if you're specifically wanting to do this manually, it's maybe not the right option.


you can use <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Properties.html" rel="nofollow">Properties</a> class to load the string and find any value from it

String readString = "aimtolerance = 1024;\r\n" + "model = Araarrow;\r\n" + "name = Bow and Arrows;\r\n" + "range = 450;\r\n" + "reloadtime = 3;\r\n" + "soundhitclass = arrow;\r\n" + "type = Ballistic;\r\n" + "waterexplosionclass = small water explosion;\r\n" + "weaponvelocity = 750;\r\n" + "default = 213;\r\n" + "fort = 0.25;\r\n" + "factory = 0.25;\r\n" + "stalwart = 0.25;\r\n" + "mechanical = 0.5;\r\n" + "naval = 0.5;\r\n"; readString = readString.replaceAll(";", ""); Properties properties = new Properties(); System.out.println(properties); try { properties.load(new StringReader(readString)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(properties); String requiredPropertyValue = properties.getProperty("default"); System.out.println("requiredPropertyValue : "+requiredPropertyValue);


Split the string on "default =" and then use indexOf to find the first occurrence of ";". Do a substring from 0 to the index and you have your value.

See <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html" rel="nofollow">http://docs.oracle.com/javase/7/docs/api/java/lang/String.html</a>


Using regular expressions:

private static final Pattern DEFAULT_VALUE_PATTERN = Pattern.compile("default = (.*?);"); private String extractDefaultValueFrom(String text) { Matcher matcher = DEFAULT_VALUE_PATTERN.matcher(text); if (!matcher.find()) { throw new RuntimeException("Failed to find default value in text"); } return matcher.group(1); }


  • How to convert numeric number into datatime
  • Android Studio onClick button to display ImageView in another activity
  • find out the largest element from the given row to the end
  • Matlab - Finding values within a matrix
  • Return only columns containing NA in R
  • How to set an indexed value in a matrix based on another matrix's values
  • Vue.js / webpack creates no build file?
  • How to cache images only in disk using Kingfisher?
  • Matlab imwrite() quality
  • Select multiple items in JTextPane
  • is there anyway to pass back additional data in jqgrid treeGrid url request?
  • npm thinks node is out of date, but it isn't
  • Deploy same Javascript webapp build to different environments
  • C# where to add a method
  • javascript add operation returns bad result
  • Textbox validation in jquery
  • Why is it ambiguous to call overloaded ambig(long) and ambig(unsigned long) with an integer literal?
  • use a single handler for multiple inputs onChange events
  • vi mode to emacs mode while on R
  • WebApp in AppServices vs CloudService
  • Swipe Gesture Recognizer not working for me
  • Android application not restoring state when installed from .apk, works fine from eclipse
  • Python cosine function precision [duplicate]
  • HttpClient: disabling chunked encoding
  • Trying to get the char code of ENTER key
  • Reading a file into a multidimensional array
  • Where to put my custom functions in Wordpress?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Buffer size for converting unsigned long to string
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • LevelDB C iterator
  • Sorting a 2D array using the second column C++
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal