Java input

550 views
198 views

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
550
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java input

  1. 1. Java Input Prepared by: Jean Michael Castor
  2. 2. Introduction • For some unfathomable reason, Java has never made it very easy to read data typed in by the user of a program.
  3. 3. Introduction • You've already seen that output can be displayed to the user using the subroutine System.out.print. This subroutine is part of a pre-defined object called System.out. The purpose of this object is precisely to display output to the user.
  4. 4. • There is also a corresponding object called System.in that exists to read data input by the user, but it provides only very primitive input facilities, and it requires some advanced Java programming skills to use it effectively. • To use System.in, a Java Scanner is used that will get the inputs.
  5. 5. Scanner Creation/Declaration Scanner keyboard = new Scanner(System.in); • Scanner is a class which must be instantiated before it can be used. In other words, you must make a new Scanner if you want to use Scanner. A reference must be used to store the location in memory of the Scanner object created. • System.in is the parameter passed to the Scanner constructor so that Java will know to connect the new Scanner to the keyboard. keyboard is a reference that will store the location of newly created Scanner object. Reference variable Object Instantiation
  6. 6. Scanner Imports • In order to use Scanner, you must import java.util.Scanner. import java.util.Scanner;
  7. 7. Scanner Methods NAME USE nextInt(); Returns the next integer value nextDouble(); Returns the next double value nextFloat(); Returns the next float value nextLong(); Returns the next long value nextShort(); Returns the next short value next(); Returns the next one word String value nextLine(); Returns the next multiple word String value
  8. 8. Reading in Integers Scanner keyboard = new Scanner(System.in); System.out.print(“ Enter Value”); int num = keyboard.nextInt(); • The nextInt() method is used to tell a Scanner object to retrieve the next integer value entered. • In the example, the next integer typed in on the keyboard would be read in and placed in the integer variable num. • nextInt() will read up to the first whitespace value entered.
  9. 9. Integers int num = keyboard.nextInt(); • The nextInt() method will read in the next integer. If a non-integer value is encountered such as a decimal value, the result will be run-time exception. • keyboard is a reference that refers to a Scanner object. method callreference variabledata type identifier
  10. 10. Strings String word= keyboard.nextLine(); • The nextLine() method will read in the next text value entered. A numeric or non-numeric text value will be accepted. • In the example, the next text entered on the keyboard would be read in and placed in variable word. • The nextLine() method would read up to the first whitespace encountered. Whitespace would be any space, any tab, or any enter key. method callreference variabledata type identifier
  11. 11. Prompts System.out.print(“Enter an integer: ”); • When performing input operations, it is a must to use prompts. A prompt is a way of indicating to a user what type of data to enter. • The prompt above indicates that an integer value is expected.

×