This document discusses reading input from the keyboard in Java using the Scanner class. It introduces streams as sequences of data that can be used for input and output. The System class contains static fields for standard input and output streams connected to the keyboard and display. The Scanner class can be used to read input by creating a Scanner instance with System.in, then calling methods like next(), nextInt(), etc. to retrieve input as strings or primitive types. An example demonstrates reading a name, age, and sex from the keyboard or in a single line using whitespace as a delimiter.
This document discusses reading input from the keyboard in Java using the Scanner class. It introduces streams as sequences of data that can be used for input and output. The System class contains static fields for standard input and output streams connected to the keyboard and display. The Scanner class can be used to read input by creating a Scanner instance with System.in, then calling methods like next(), nextInt(), etc. to retrieve input as strings or primitive types. An example demonstrates reading a name, age, and sex from the keyboard or in a single line using whitespace as a delimiter.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10
Object-Oriented Programming (CS F213)
Module I: Object-Oriented and Java Basics
CS F213 RL 6.6: Reading From Console in Java
BITS Pilani Dr. Pankaj Vyas
Department of Computer Science, BITS-Pilani, Pilani Campus CS F213 RL 6.6 : Topics
Reading Inputs from Keyboard in Java
What is Stream Introduction to System class Reading Inputs From Keyboard via Scanner class
2 Object-Oriented Programming (CS F213)
What is Stream? Stream is a sequence of data. [Channel Through which data moves] Program may require to read data from various sources [Keyboard, Files, etc.) Programs may require to write the data to various destinations [Console, Printer, Files etc.] InputStream Reads Data From Data Sources. [Data Moves from Data Source to Program]
OutputStream Writes Data to Destinations. [Data Moves from
Program to Data Destination]
3 Object-Oriented Programming (CS F213)
System class in Java
<public> and <final> class in java.lang package.
public final class System extends Object
{ System.in is "standard" input stream. This stream is already open and ready static InputStream in; to supply input data. Typically this stream corresponds to keyboard input
System.out "standard" output stream.
This stream is already open and ready static PrintStream out; to accept output data. Typically this stream corresponds to display output over console. }// End of class System
4 Object-Oriented Programming (CS F213)
Reading Input Using Scanner Scanner class is introduced in Java 1.5 To use Scanner class, import java.util package Important Constructor Methods of Scanner class Scanner(File source) Constructs a new Scanner that produces values scanned from the specified file. Scanner(InputStream source) Constructs a new Scanner that produces values scanned from the specified input stream. Steps to Use Scanner class for keyboard input Step 1: Create a Scanner Instance using System.in as parameter Scanner sc = new Scanner(System.in); Step 2: Use a suitable Scanner class Method shown in the next slide Scanner class reads input in the form of tokens. A token is a block of text which may contain values of multiple fields separated via delimiters. A delimiter is a character that separates the values of different fields. Whitespace character is the default delimiter. [Genral Demiliters are , ; etc] Examples: Suppose you reading the name, age and sex of a person as an input in a single line. David 20 M (white space default delimiter) David,20,M (comma is used as delimiter)
5 Object-Oriented Programming (CS F213)
Important Scanner class Methods String next() Finds and returns the next complete token from this scanner. boolean nextBoolean() Scans the next token of the input into a boolean value and returns that value. byte nextByte() Scans the next token of the input as a byte. double nextDouble() Scans the next token of the input as a double. float nextFloat() Scans the next token of the input as a float. int nextInt() Scans the next token of the input as an int. String nextLine() Advances this scanner past the current line and returns the input that was skipped. long nextLong() Scans the next token of the input as a long. short nextShort() Scans the next token of the input as a short.
6 Object-Oriented Programming (CS F213)
Scanner class Example // File Name: Test.java // Program Reads Name, age and sex of a person and then displays the read values on console import java.util.*; // To use Scanner class class Test { public static void main(String args[]) { // Step 1 : Create a Scanner Instance using System.in Scanner sc = new Scanner(System.in);
System.out.println("Enter Name, Age and Sex of the Person");
// Step 2 : Use Suitable Scanner class Methods
String name = sc.next(); // Reads Name
int age = sc.nextInt(); // Reads Age String sex = sc.next(); // Reads Sex
System.out.println("Name : " + name + "Age: "+age+" Sex: "+sex);
}// End of Method
}// End of class Test 7 Object-Oriented Programming (CS F213) Scanner class Example You can Input the value of each field in a separate line or on the same line using whitespace character as a delimiter
Inputs in Separate Lines Inputs in Same Line
F:\>java Test F:\>java Test Enter Name, Age and Sex of the Person Enter Name, Age and Sex of the Person David David 30 M 30 Name : DavidAge: 30 Sex: M M Name : DavidAge: 30 Sex: M
8 Object-Oriented Programming (CS F213)
Scanner class Example Mismatched Type Values May Results into Exceptions F:\>java Test Enter Name, Age and Sex of the Person David S 20 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at Test.main(its.java:14)