![]() A similar method String. Using equalsIgnoreCase(), two strings are considered equal if they are of the same length and corresponding characters in the two strings are equal, ignoring their cases. ![]() ![]() Try (Scanner reader = new Scanner(new File("test. The Java String.equalsIgnoreCase() compares the current string with the specified string in a case-insensitive manner. * `Try With Resources` used here to auto-close() the reader false The file line contains only "false" Īgain.read the comments in code: // List Interface to hold true/false file line results:.true The file line contains only "true".Below is a brief explanation of the regex used: You will also note that the String#matches() method is used with a Regular Expression (regex) string to validate the fact that a read file line contains either "true" or "false". It should be noted that, in this use case, the file lines are trimmed of white-spacing etc as the file lines are read. I will suggest however, that you don't use an array at all but instead, use a List Interface or an ArrayList to hold file line results which can grow dynamically and wont possibly end up being full of null or empty elements (if you don't want it to).īelow is an example of how a List Interface can be used. The String.equals() in Java compares a string with the object passed as the method argument. It verifies the object references, not the content, which is undesirable in most cases. You've already been explained (quite nicely) the problem with arbitrarily declaring an array to hold values where you possibly have no idea how many elements you may need to read the entire file of boolean values so I won't do that again here. Never use '' operator for checking the strings equality.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |