Page 331 - CITS - Computer Software Application -TT
P. 331
COMPUTER SOFTWARE APPLICATION - CITS
• Uses a while loop to continuously check if the next input is an integer using sc.hasNextInt(). If an integer
is available, it reads the integer value using sc.nextInt(), adds it to the sum, and increments the count.
3 After the loop, it checks whether any integers were input (i.e., count > 0). If count is greater than zero, it
calculates the mean (average) by dividing sum by count and then prints the mean value. If no integers were
input (i.e., count is still zero), it prints a message stating that the mean cannot be calculated.
Input
1
2
3
4
5
Output
Mean: 3
• Importing the Class: To use the Scanner class, you need to import it with import java.util.Scanner;.
• Reading from Standard Input: The Scanner class is commonly used to read input from the standard input
stream (System.in) by creating a Scanner object with System.in as the argument.
• Reading from Files: You can also read input from files by passing a File object as an argument to the Scanner
constructor.
• Reading Different Data Types: Scanner provides methods like nextByte(), nextInt(), nextLong(), nextFloat(),
nextDouble(), etc., to read different data types.
• Reading Strings: To read strings, you can use next() or nextLine() methods. next() reads the next token
(usually a word), while nextLine() reads the entire line.
• Reading Characters: You can read a single character by combining next() and charAt(0), as in next().charAt(0).
• Delimiter: By default, Scanner uses whitespace as the delimiter to separate tokens. You can change the
delimiter using the useDelimiter() method.
• Checking for Input: You can check if there is more input to read with methods like hasNext(), hasNextInt(),
hasNextLine(), etc.
• Closing the Scanner: It’s important to close the Scanner object using close() when you’re done with it to
release resources.
• Handling Input Errors: Always validate input using methods like hasNextInt() or exception handling to prevent
unexpected program behaviour due to invalid input.
• Tokenization: Scanner reads input and tokenizes it. Tokens are smaller units of input separated by the specified
delimiter.
• Locale and Locale-Sensitive Parsing: You can set the locale to influence how Scanner parses numbers and
strings. For example, comma (,) or period (.) as a decimal separator.
• Not Suitable for Competitive Programming: While Scanner is convenient, it may not be the most efficient
choice for scenarios where input processing time is critical, such as competitive programming.
• Exception Handling: Be prepared to handle exceptions like InputMismatchException or NoSuchElementException
that may occur if the input doesn’t match the expected format.
• Thread Safety: Scanner is not thread-safe, so avoid sharing Scanner objects between multiple threads without
proper synchronisation.
• Unicode Support: Scanner supports Unicode characters, allowing you to read and process text in various
languages.
318
CITS : IT&ITES - Computer Software Application - Lesson 78 - 84