100 likes | 114 Views
Understand how to handle keyboard and screen input/output in Java using System.out and Scanner class, along with methods for reading various data types.
E N D
Screen Output • System.out is an object that is part of the Java language. (Don’t worry yet about why there is a dot in the name of this object.) • One of the methods of System.out is println. • In this case, the argument you give println is the string you want to output.
Screen Output (cont’d) • Another method of System.out is print. • The difference between println and print is that println goes to a new line after printing the string and print does not. • For example: System.out.print(“One, two,”); System.out.print(“ buckle my shoe.”); System.out.println(“ Three, four,”); System.out.println(“ shut the door.”); will produce: One, two, buckle my shoe. Three, four, shut the door.
Keyboard Input • Keyboard input is done using the Scanner class, which is in the java.util package. (util is short for utility.) • A package is simply a library of classes. • In order to make the java.util package available to your program, you must place the following line must be inserted at the beginning of your program: import java.util.*;
Keyboard Input (cont’d) • To handle keyboard input you can create an object of the class Scanner as follows: Scanner Scanner_Object_name = new Scanner(System.in); where Scanner_Object_name is any valid Java identifier. • For example, if keyboard is the identifer, we can write: Scanner keyboard = new Scanner(System.in);
Keyboard Input (cont’d) • After this line appears you can use methods of the Scanner class with the object defined to read data that the user types in a the keyboard. • For example, the method nextInt reads one int value typed in at the keyboard. • The method actually reads in the next string of characters typed in, up to the next blank, and converts that string to an integer.
Methods in the Class Scanner • Scanner_Object_Name.next() • Returns the String value consisting of the next keyboard characters up to, but not including, the first delimiter character (by default that is a blank). • Scanner_Object_Name.nextLine() • Returns the rest of the current keyboard input line and returns the characters read as value of type String. Note that the line terminator ‘\n’ is read and discarded; it is not included in the string returned.
Methods in the Class Scanner (cont’d) • Scanner_Object_Name.nextInt() • Returns the next value of type int that is typed in at the keyboard. • Scanner_Object_Name.nextLong() • Returns the next value of type long that is typed in at the keyboard. • Scanner_Object_Name.nextByte() • Returns the next value of type byte that is typed in at the keyboard. • Scanner_Object_Name.nextShort() • Returns the next value of type short that is typed in at the keyboard.
Methods in the Class Scanner (cont’d) • Scanner_Object_Name.nextDouble() • Returns the next value of type double that is typed in at the keyboard. • Scanner_Object_Name.nextFloat() • Returns the next value of type float that is typed in at the keyboard. • Scanner_Object_Name.nextBoolean() • Returns the next value of type boolean that is typed in at the keyboard. The values of trueandfalse are entered as the strings “true” and “false”. Any combination of upper- and/or lower-case letters is allowed in spelling “true” and “false”.
Methods in the Class Scanner (cont’d) • Scanner_Object_Name.useDelimiter (Delimiter_Word) • Makes the string Delimiter_Word the only delimiter used to separate input. That is, blanks and other white space will no longer be delimiters (unless they are part of Delimiter_Word).