The Java Scanner class allows user input through methods like nextInt(), nextDouble(), and nextLine(). It is imported from the java.util package. To use Scanner, an object is created passing System.in, and methods are called on that object to read input as different data types like int, double, and string. Examples demonstrate using nextInt() to read an integer, nextDouble() to read a double, and nextLine() to read a string.
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
0 ratings0% found this document useful (0 votes)
16 views
Lecture 2
The Java Scanner class allows user input through methods like nextInt(), nextDouble(), and nextLine(). It is imported from the java.util package. To use Scanner, an object is created passing System.in, and methods are called on that object to read input as different data types like int, double, and string. Examples demonstrate using nextInt() to read an integer, nextDouble() to read a double, and nextLine() to read a string.
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/ 7
Java Scanner Class
Mateen Ahmed Abbasi
Department of Computer Science Khwaja Fareed University Of Engineering and Information Technology Scanner Class The most common method of taking an input in a java program is through Scanner Class which is imported from the Java Utility Package by using the statement given below. import java.util.Scanner; /*imported at beginning of the java program*/ The Input is given with the help input stream System.in Syntax: Scanner sc=new Scanner(System.in); /*Where sc is an object name, you can change it as per your choice*/ Cont… There are different input methods provided in Scanner class for different primitive data types such as:
Data Type Method
Integer nextInt() Double nextDouble() Long nextLong() Float nextFloat() Byte NextByte() String nextLine() /*Allows Spaces between a String */ next() /*Won’t Allow Spaces between a String */ Java User Input The Scanner class is used to get user input, and it is found in the java.util package. To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings: import java.util.Scanner; // Import the Scanner class
public class Main {
public static void main(String[] args) { // Create a Scanner object Scanner myObj = new Scanner(System.in); System.out.println("Enter username"); // Read user input String userName = myObj.nextLine(); // Output user input System.out.println("Username is: " + userName); } } Example 2: Java Scanner nextInt() import java.util.Scanner; public class Main { public static void main(String[] args) { // creates a Scanner object Scanner input = new Scanner(System.in); System.out.println("Enter an integer: "); // reads an int value int data1 = input.nextInt(); System.out.println("Using nextInt(): " + data1); } } Example 3: Java Scanner nextDouble() import java.util.Scanner; public class Main { public static void main(String[] args) { // creates an object of Scanner Scanner input = new Scanner(System.in); System.out.print("Enter Double value: "); // reads the double value double value = input.nextDouble(); System.out.println("Using nextDouble(): " + value); } }