Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Lecture 11 Getting Input from the users in JAVA

The document discusses methods for taking user input in Java, highlighting the Scanner class as the preferred approach for reading various types of input. It explains the behavior of different input methods, such as nextInt() and nextLine(), and their interactions with whitespace characters. Additionally, it touches on command line arguments for input and poses practice programming exercises related to user input.

Uploaded by

krishnathakre925
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 11 Getting Input from the users in JAVA

The document discusses methods for taking user input in Java, highlighting the Scanner class as the preferred approach for reading various types of input. It explains the behavior of different input methods, such as nextInt() and nextLine(), and their interactions with whitespace characters. Additionally, it touches on command line arguments for input and poses practice programming exercises related to user input.

Uploaded by

krishnathakre925
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

JAVA Programming (CST015)

Lecture 11: Taking Inputs from users in JAVA

Department of Computer Science and Engineering


National Institute of Technology, Srinagar, Jammu and Kashmir
April 03, 2024
Getting Input from Users
• Taking Input from users adds interactivity to the programs.
• So, there are many ways in JAVA using which the inputs from the
users can be taken in a JAVA Program.
• However, if we compare it with other programming languages like
PYTHON, taking and reading the data entered by the users is not that
easy.
• You know that System.out.print() is used to display some message
and output to the user.
• There is also corresponding System.in object to read the data input
by the users. However, it provides very limited input facilities.
1. Scanner Class
• So, System.in can be used with the Scanner Class.
• The Scanner class is the predefined class located in JAVA pre-defined
libraries.
• This is probably the most preferred method to take input in JAVA.
• Scanner class is a class in java.util package used for taking the user
inputs.
• Scanner Class is used to read input from various sources like the
console, files, Strings, etc.
• To use the Scanner class, create an object of the Scanner class and
pass the predefined object System.in, which represents standard
input stream, as the input source.
What will be the Output if the user enters 12.34 as input?
import java.util.Scanner;

public class UserInputExample {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.print("Enter a number: ");
int number = sc.nextInt();
System.out.println("You entered: " + number);
sc.close();
}
}
What will be the Output if the user enters 12.34 as input?
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("You entered: " + number);
scanner.close();
}
}

The code will throw an InputMismatchException because the input


"12.34" is a floating-point number and cannot be assigned to an
integer variable
Output if the user enters “hello World” as input?
import java.util.Scanner;

public class UserInputExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter a string: ");


String str = sc.next();

System.out.println("You entered: " + str);


sc.close();
}
}
Solution: Output if the user enters “hello World” as input?
import java.util.Scanner;

public class UserInputExample {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string: ");


String str = scanner.next();
System.out.println("You entered: " + str);
scanner.close();
}
}
The next() method will only read the next token, which is "Hello", and not the entire string.
Parsing Strings using Scanner Class
• We can also pass String object to the Scanner Class Constructor.
• Using Scanner class, we can parse the Strings.

Scanner sc= new Scanner(“Hello world. How are you”);


//Here Scanner class is reading the input from the String.

System.out.prinln(sc.next()); //prints Hello


System.out.prinln(sc.next()); //prints world.
System.out.prinln(sc.nextLine()); //prints How are you
Parsing Strings using Scanner Class

Scanner sc= new Scanner(“Hello 1 world. How are you”);

System.out.prinln(sc.next()); //prints Hello


System.out.prinln(sc.nextInt()); //prints 1
System.out.prinln(sc.nextInt()); //Exception InputMismatch
Output?
import java.util.Scanner;

public class UserInputExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();

System.out.print("Enter a string: ");


String str = sc.next();

System.out.println("You entered: " + number + " and " + str);


scanner.close();
}
}
Output?
import java.util.Scanner;

public class UserInputExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt(); //23 43

System.out.print("Enter a string: ");


String str = sc.nextLine();

System.out.println("You entered: " + number + " and " + str);


scanner.close();
}
}
Answer to Previous Slide Question
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
System.out.print("Enter a string: ");
String str = sc.nextLine();
System.out.println("You entered: " + number + " and " + str);
sc.close();
}
}

This is because sc.nextInt() only reads the integer value and does not consume the newline
character.
The call to sc.nextLine() will read the leftover newline character instead of the intended input
string.
Explanation to Previous Question
• Scanner class in Java treats whitespace characters (including the
newline character) as delimiters by default.
• The next() method reads the next input token until it encounters a
delimiter (whitespace character) and returns the token as a string,
excluding the delimiter.
• In the case of nextLine(), it reads the entire line of input, including
the newline character at the end, and returns it as a string.
• Therefore, when you use sc.nextLine() to read input, it reads the
input line and also consumes the newline character left in the input
buffer.
Output?
import java.util.Scanner;

public class UserInputExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();

sc.nextLine();

System.out.print("Enter a string: ");


String str = sc.nextLine();

System.out.println("You entered: " + number + " and " + str);


scanner.close();
}
}
2. Input from Command line
• Users can also provide inputs using the command line arguments.
javac Main.java
java Main hello world how are you
Practice Programs
1. WAP that will accept two integer values as command line argument
and outputs the sum of the two entered values.
Why?
We know that the Scanner class is a predefined class located in the
java.util package,
and similarly, the Math class is also a predefined class in Java located in
the java.lang package.

Why do we need to import the Scanner class in our Java program, but
not the Math class when we use it?

You might also like