0% found this document useful (0 votes)
8 views

User Input

Programming Language Java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

User Input

Programming Language Java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Java User Input

1
User input in Java
● Sometimes we need to take input from the user.
● Creates a connection between the program and it’s user.
● Most common way to take input is using the java scanner class.

2
What is a Java Scanner Class?
● A predefined class that reads data from the keyboard or a text file.
● Found in the java.util package.
● Can read all types of data (numeric, strings and other data types).
● Easiest way to take input from the user.

3
User Input using Scanner class
● Import the java.util.Scanner package
● Create an object of the Scanner class
● Syntax:
To create an object
Class Name of Scanner class

Scanner sc = new Scanner (System.in);

Variable Name
To receive input
from user

4
Example
1. import java.util.Scanner;
2. class UserInput {
3. public static void main (String [] args){
4. Scanner sc = new Scanner (System.in);
5. String st1 = sc.nextLine();
6. System.out.println("Output: " + st1);
7. }
8. }

Welcome to DrJava. Working directory is C:\Users\Desktop


> Hello World
Output: Hello World

5
Inputting Different Datatypes

Method Usage

nextInt() Reads an integer value from the user

nextLong() Reads a long value from the user

nextFloat() Reads a float value from the user

nextDouble() Reads a double value from the user

nextBoolean() Reads a boolean value from the user

nextLine() Reads a string value from the user

next() Reads a string value until a space (“ “) is encountered

6
Inputting Integer Datatype

1. import java.util.Scanner;
2. public class UserInput {
3. public static void main (String [] args){
4. Scanner sc = new Scanner (System.in);
5. System.out.print(“Enter an integer: “);
6. int num1 = sc.nextInt();
7. System.out.println(“Output: ” + num1);
8. }
9. }

Welcome to DrJava. Working directory is C:\Users\Desktop


65
> Enter an integer:
Output: 65
7
Inputting Boolean Datatype

1. import java.util.Scanner;
2. public class UserInput {
3. public static void main (String [] args){
4. Scanner sc = new Scanner (System.in);
5. System.out.print(“Enter a boolean: “);
6. boolean bool = sc.nextBoolean();
7. System.out.println(“Output: “ + bool);
8. }
9. }

Welcome to DrJava. Working directory is C:\Users\Desktop


true
> Enter a boolean:
Output: true
8
Inputting String Datatype

1. import java.util.Scanner;
2. public class UserInput {
3. public static void main (String [] args){
4. Scanner sc = new Scanner (System.in);
5. System.out.print(“Enter a string: “);
6. String st1 = sc.nextLine();
7. System.out.println(“Output: “ + st1);
8. }
9. }

Welcome to DrJava. Working directory is C:\Users\Desktop


Hello there CSE110
> Enter a string:
Output: Hello there CSE110
9
Inputting String Datatype (Contd.)

1. import java.util.Scanner;
2. public class UserInput {
3. public static void main (String [] args){
4. Scanner sc = new Scanner (System.in);
5. System.out.print(“Enter a string: “);
6. String st1 = sc.next();
7. System.out.println(“Output: “ + st1);
8. }
9. }

Welcome to DrJava. Working directory is C:\Users\Desktop


Hello there CSE110
> Enter a string:
Output: Hello
10
THANK YOU!

11

You might also like