1 / 10

Java Input Output Basics: Keyboard and Screen I/O

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.

dyal
Télécharger la présentation

Java Input Output Basics: Keyboard and Screen I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Keyboard and Screen I/O (Input/Output)

  2. 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.

  3. 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.

  4. 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.*;

  5. 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);

  6. 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.

  7. 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.

  8. 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.

  9. 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”.

  10. 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).

More Related