Week 2 Scanner.pdf
Document Details
Uploaded by SimplifiedLaplace
Tags
Full Transcript
Scanner Class Introduction • We know we need variables to hold values in our program • These values can be hardcoded or it is possible to allow the user of the program to enter their own values, and for the program to act on these values. • Java 5 and above uses the Scanner class to achieve this...
Scanner Class Introduction • We know we need variables to hold values in our program • These values can be hardcoded or it is possible to allow the user of the program to enter their own values, and for the program to act on these values. • Java 5 and above uses the Scanner class to achieve this Add2Numbers.java public class Add2Numbers { public static void main(String[] args) { //declare variables int num1, num2, total; //assign integer values to num1 and num2 num1 = 10; num2 = 33; //add numbers together and assign to total total = num1 + num2; //display result on screen System.out.println("total = " + total); }//end main method }//end class • Problem with this program • Result will always be 43 • Need to be able to input data at runtime The Scanner class • Java has a class called Scanner • Scanner class allows us to read keyboard input in a convenient manner • Scanner class is part of a package called util • A package is a collection of pre-compiled classes • In order to make the Scanner class accessible to the compiler we have to tell it to look in the util package import java.util.Scanner; OR import java.util.*; • Asterisk * means that all the classes in the util package are available to compiler The Scanner class • To use the Scanner class you first need to create a Scanner object class constructor Scanner keyboardIn = new Scanner(System.in); object parameter • The Scanner object keyboardIn is then used to read input from the keyboard. The Scanner class • To construct a Scanner object, pass the System.in object to the Scanner constructor • System.in represents the keyboard so keyboardIn knows we are getting data from keyboard as opposed to disk or file Scanner keyboardIn = new Scanner(System.in) The Scanner class • The Scanner class has several input methods, each one associated with a different input type The input methods of the Scanner class Java type int long float double String nextType method nextInt() nextLong() nextFloat() nextDouble() nextLine() Scanner class methods • nextInt() reads an integer value • nextDouble() reads a double • nextLine() reads a line • (until user hits Enter) • next() reads line up until white space Scanner methods • To read in an integer : method object int num; num = keyboardIn.nextInt(); • The method nextInt() is called on the Scanner object (keyboardIn) and assignment operator (=) used to assign the value read in to a variable (num) of correct type (int) Scanner methods • To read in an integer use: num = keyboardIn.nextInt(); • To read in a double use: price = keyboardIn.nextDouble(); • To read in a string of characters use: nextLine() or next() name = keyboardIn.nextLine(); name = keyboardIn.next(); • The nextLine() method reads a line (up until it reaches return) • The next() method reads a line up until it reaches a white space Example Scanner keyboardIn = new Scanner(System.in); int age; //prompt user to enter value System.out.print("Enter your age: "); //read input from keyboard and assign to int variable age age = keyboardIn.nextInt(); //display age on screen System.out.print("You are " + age + " years old"); Example 1 // Import packages containing predefined classes import java.util.*; public class IntegerInput { /* main method */ public static void main(String[] args) { // create instance of Scanner class Scanner keyboardIn = new Scanner(System.in); //declare variables int num1, num2; // prompt for input System.out.print("Enter first number: "); // read in integers num1 = keyboardIn.nextInt(); System.out.print("Enter 2nd number: "); num2 = keyboardIn.nextInt(); // output the result System.out.print("The values entered are "); System.out.println(num1 + " and " + num2); }// end main method }// end class Problem - Reading character input • No nextChar() method Scanner method char c; c = keyboardIn.next().charAt(0); Scanner object String method Example 2 import java.util.Scanner; public class Initials { public static void main(String [] args) { Scanner keyboardIn = new Scanner(System.in); //Declare variables char firstInitial, secondInitial; //Get values from user System.out.print("Enter your first initial: "); firstInitial = keyboardIn.next().charAt(0); System.out.print("Enter your second initial: "); secondInitial = keyboardIn.next().charAt(0); //Print initials to screen System.out.print("Your initials are "+firstInitial+ " "+secondInitial); } } String (basics) • A string literal is a sequence of characters enclosed in quotation marks – • Strings are objects of the String class – • “Hello world!” String is a class – starts with uppercase S Declare an object of class String String firstName = new String(); » Calls constructor of empty string OR String firstName; OR String firstName = "Paul"; • Assign a value to a string firstName = “Mary"; The String class • A String example: String firstName; //declare string object firstName = “Mary"; //assign a value to firstName System.out.println("Name: " +firstName); Example 3 // Import packages containing predefined classes import java.util.*; class HelloWithInput { /**main method*/ public static void main(String[] args) { //create instance of scanner class Scanner keyboardIn = new Scanner(System.in); //declare variables String firstName; String surname; //get input System.out.print("What is your first name: "); firstName = keyboardIn.nextLine(); System.out.print("what is your surname: "); surname = keyboardIn.nextLine(); //output System.out.println("Hello " +firstName + " " + surname); System.out.println("Welcome to the Scanner class!"); }//end main method }//end class