
Sometimes you need to scan line-by-line, with multiple tokens on a line.
Java Tutorials/Essential Classes/Regular Expressions. hasNext(String pattern) - Returns true if the next token matches the pattern constructed from the specified string. Note that it's trivial to make the above test case-insensitive: just provide such regex pattern to the Scanner. In regex, as a Java string literal, the pattern "" is what is called a "character class" it matches any of the letters a, e, i, o, u. Here's an example of using it to validate vowels. Scanner has many advanced features supported by regular expressions. More often than not you'd want to do the more specific test before the more general test. If a Scanner hasNextInt(), then it also hasNextLong(), but it's not necessarily true the other way around. Note that the order of the tests matters. Sc.hasNextBoolean() ? "(boolean) " + sc.nextBoolean() : Sc.hasNextDouble() ? "(double) " + sc.nextDouble() : Sc.hasNextLong() ? "(long) " + sc.nextLong() : Sc.hasNextInt() ? "(int) " + sc.nextInt() : Here's an example of performing multiple hasNextXXX tests. If one hasNextXXX test fails, you can still test if it perhaps hasNextYYY!. If you need to skip the "garbage" input that fails your hasNextXXX test, then you need to advance the Scanner one way or another (e.g. It's important to realize that none of the hasNextXXX methods advance the Scanner past any input! You will find that if you omit this line from the snippet, then it'd go into an infinite loop on an invalid input! Note that the snippet above contains a sc.next() statement to advance the Scanner until it hasNextInt(). How do I keep a scanner from throwing exceptions when the wrong type is entered? (java)Įxample 2: Multiple hasNextXXX on the same token. How to use Scanner to accept only valid int as input.By contract, a Scanner guarantees that if it hasNextInt(), then nextInt() will peacefully give you that int, and will not throw any NumberFormatException/ InputMismatchException/ NoSuchElementException.
Note how much easier Scanner.hasNextInt() is to use compared to the more verbose try/catch Integer.parseInt/ NumberFormatException combo. Here's a simple example of using hasNextInt() to validate positive int from the input. The following discussion will keep the regex as simple as possible, so the focus remains on Scanner. There are also find and skip methods that ignores delimiters. One important feature is useDelimiter(String pattern), which lets you define what pattern separates your tokens.
Scanner is capable of more, enabled by the fact that it's regex-based. hasNext(Pattern pattern) is the pile overload.The integral types also has overloads to specify radix (for e.g.
As bonus, there's also hasNextBigInteger() and hasNextBigDecimal(). Also available are hasNextDouble(), hasNextFloat(), hasNextByte(), hasNextShort(), hasNextLong(), and hasNextBoolean(). hasNextInt() - does it have a token that can be parsed into an int?. hasNextLine() - does it have another line of input?. hasNext() - does it have any token at all?. has many hasNextXXX methods that can be used to validate input. This will start ksar and you may load the sar files for having a look at the statistics:Īnother option is to generate a pdf: java -jar kSar.jar -input '/var/log/sa/sarXX' -outputPDF today.pdfĪnd even faster: create a bash function and an alias in your. Installing kSar is just a matter of unzipping the provided package and either executing the run.sh script or use java directly to execute the jar file: java -jar kSar.jar this can be very handy if you are looking for peaks and want to have a quick overview what happened on your system. If you are on linux/solaris and sar is configured and running on your system there is a nice utility called kSar which can be used to create graphs of the various statistics sar gathered.