module3
module3
In Java, there are many ways to read strings from input. The simplest one is to make use of
the class Scanner, which is part of the java.util library and has been newly introduced in
Java 5.0. Using this class, we can create an object to read input from the standard input
channel System.in (typically, the keyboard), as follows:
import java.util.Scanner; - imports the class Scanner from the library java.util
Scanner scanner = new Scanner(System.in); - creates a new Scanner object, that is
connected to standard input (the keyboard)
String inputString = scanner.nextLine();
1. temporarily suspends the execution of the program, waiting for input from the
keyboard;
2. the input is accepted as soon as the user digits a carriage return;
3. (a reference to) the string entered by the user (up to the first newline) is
returned by the nextLine() method;
4. (the reference to) the string is assigned to the variable inputString.
We can also read in a single word (i.e., a sequence of characters delimited by a whitespace
character) by making use of the next() method of the Scanner class. We will see later that
the Scanner class is also equipped with other methods to directly read various types of
numbers from standard input.
https://www.inf.unibz.it/~calvanese/teaching/06-07-ip/lecture-notes/uni02/node33.html
SCANNER CLASS
Java Scanner
Scanner class in Java is found in the java.util package. Java provides various ways to read
input from the keyboard, the java.util.Scanner class is one of them.
The Java Scanner class breaks the input into tokens using a delimiter which is whitespace by
default. It provides many methods to read and parse various primitive values.
The Java Scanner class is widely used to parse text for strings and primitive types using a
regular expression. It is the simplest way to get input in Java. By the help of Scanner in Java,
we can get input from the user in primitive types such as int, long, double, byte, float, short,
etc.
The Java Scanner class extends Object class and implements Iterator and Closeable
interfaces.
The Java Scanner class provides nextXXX() methods to return the type of value such as
nextInt(), nextByte(), nextShort(), next(), nextLine(), nextDouble(), nextFloat(),
nextBoolean(), etc. To get a single character from the scanner, you can call next().charAt(0)
method which returns a single character.
To get the instance of Java Scanner which reads input from the user, we need to pass the
input stream (System.in) in the constructor of Scanner class. For Example:
To get the instance of Java Scanner which parses the strings, we need to pass the strings in
the constructor of Scanner class. For Example:
SN Constructor Description
Example 1
Let's see a simple example of Java Scanner where we are getting a single input from the user.
Here, we are asking for a string through in.nextLine() method.
1. import java.util.*;
2. public class ScannerExample {
3. public static void main(String args[]){
4. Scanner in = new Scanner(System.in);
5. System.out.print("Enter your name: ");
6. String name = in.nextLine();
7. System.out.println("Name is: " + name);
8. in.close();
9. }
10. }
Output:
https://www.javatpoint.com/Scanner-class
DISPLAYING OUTPUT
In this tutorial, you will learn simple ways to display output to users and take input from
users in Java.
Java Output
System.out.println(); or
System.out.print(); or
System.out.printf();
Here,
System is a class
out is a public static field: it accepts output data.
Don't worry if you don't understand it. We will discuss class, public, and static in later
chapters.
Let's take an example to output a line.
class AssignmentOperator {
public static void main(String[] args) {
Output:
class Output {
public static void main(String[] args) {
Output:
1. println
2. println
1. print 2. print
In the above example, we have shown the working of the print() and printf() methods. To
learn about the printf() method, visit Java printf().
class Variables {
public static void main(String[] args) {
System.out.println(5);
System.out.println(number);
}
}
5
-10.6
Here, you can see that we have not used the quotation marks. It is because to display integers,
variables and so on, we don't use quotation marks.
Example: Print Concatenated Strings
class PrintVariables {
public static void main(String[] args) {
Output:
I am awesome.
Number = -10.6
Here, we have used the + operator to concatenate (join) the two strings: "I am
" and "awesome.".
And also, the line,
Here, first the value of variable number is evaluated. Then, the value is concatenated to the
string: "Number = ".
Java Input
Java provides different ways to get input from the user. However, in this tutorial, you will
learn to get input from user using the object of Scanner class.
In order to use the object of Scanner, we need to import java.util.Scanner package.
import java.util.Scanner;
To learn more about importing packages in Java, visit Java Import Packages.
Then, we need to create an object of the Scanner class. We can use the object to take input
from the user.
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Output:
Enter an integer: 23
You entered 23
In the above example, we have created an object named input of the Scanner class. We then
call the nextInt() method of the Scanner class to get an integer input from the user.
Similarly, we can use nextLong(), nextFloat(), nextDouble(), and next() methods to
get long, float, double, and string input respectively from the user.
Note: We have used the close() method to close the object. It is recommended to close the
scanner object once the input is taken.
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Output:
https://www.programiz.com/java-programming/basic-input-output