1import java.util.Scanner;
2
3// import scanner
4
5Scanner myScanner = new Scanner(System.in); // Make scanner obj
6
7String inputString = myScanner.nextLine(); // Take whole line
8
9boolean inputBoolean = myScanner.nextBoolean(); //Boolean input
10
11long inputLong = myScanner.nextLong(); //Interger,long ... input
1import java.util.Scanner; //Import Scanner in java
2
3class classname{
4 public void methodname(){
5 Scanner s_name = new Scanner(System.in); //Scanner declaration
6 //Use Scanner object to take input
7 int val1 = s_name.nextInt(); //int
8 float val2 = s_name.nextFloat(); //float
9 double val3 = s_name.nextDouble(); //double
10 string name = s_name.nextLine(); //string
11 char ch = s_name.nextLine().charAt(0); //character
12 }}
1import java.util.Scanner;
2
3Public class Scanner {
4 Public static void main(String[] args) {
5 // Scanner *scanner name here* = new Scanner(System.in);
6 Scanner scan = new Scanner(System.in);
7 System.out.println("Type anything and the scanner will take that input and print it");
8 String next = scan.next();
9 System.out.println(next);
10 }
11}
1If you use the nextLine() method immediately following the nextInt() method,
2nextInt() reads integer tokens; because of this, the last newline character for
3that line of integer input is still queued in the input buffer and the next
4nextLine() will be reading the remainder of the integer line (which is empty).
5So we read can read the empty space to another string might work. Check below
6code.
7import java.util.Scanner;
8
9public class Solution {
10
11 public static void main(String[] args) {
12 Scanner scan = new Scanner(System.in);
13 int i = scan.nextInt();
14
15 // Write your code here.
16 double d = scan.nextDouble();
17 String f = scan.nextLine();
18 String s = scan.nextLine();
19
20 System.out.println("String: " + s);
21 System.out.println("Double: " + d);
22 System.out.println("Int: " + i);
23 }
24}
1// import Scanner
2import java.util.Scanner;
3
4// Initialize Scanner
5Scanner input = new Scanner(System.in);
6
7// Test program with Scanner
8System.out.println("What is your name?");
9String name = input.nextLine();
10
11System.out.println("Hello," + name + " , it is nice to meet you!");
1import java.util.Scanner;
2public class scanners{
3 public static void main(String[] args){
4 Scanner scan = new Scanner(System.in);
5 // Reading int
6 System.out.print("Enter your age");
7 int age = scan.nextInt();
8 // Reading double
9 System.out.print("Enter your salary");
10 double salary = scan.nextDouble();
11 // Reading float
12 System.out.print("Enter employees average salary");
13 float average = scan.nextFloat();
14 // Reading Single Character
15 System.out.print("Enter any character from a to z");
16 char charcter = scan.next().charAt(0);
17 // Reading string
18 System.out.print("Enter your full name");
19 String fullname = scan.next();
20 // Reading long
21 System.out.print("enter a long number");
22 long number = scan.nextLong();
23 }
24}