Scanner class:Scanner is predefinied class in java which is available in java.
util
package.
Scanner class is used to take input from user.
steps:
1) If we want to use Scanner class then we have to create object of Scanner class.
Scanner sc=new Scanner([Link]);
2) Import scannner class from [Link] package.
import [Link];
3)Methods in scanner class.
1)nextInt();
2)nextByte();
3)nextDouble();
4)nextfloat();
5)nextShort();
6)nextLong();
7)nextBoolean();
8)
4) wrong input (Input mismatch Exception)
Example 1: byte type input
package userInput;
import [Link];
public class ByteDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Byte Value");
byte b = [Link]();
[Link](b);
}
}
Example 2 : short type input
package userInput;
import [Link];
public class ShortDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Short value");
short s = [Link]();
[Link](s);
}
}
Example 3 :int input
package userInput;
import [Link];
public class IntDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Int No");
int no = [Link]();
[Link](no);
}
}
Example 4: long input
package userInput;
import [Link];
public class LongDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Long Number");
long l = [Link]();
[Link](l);
}
}
Example 5: float input
package userInput;
import [Link];
public class FloatDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter float No");
float no = [Link]();
[Link](no);
}
}
example 6: double input
package userInput;
import [Link];
public class DoubleDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Double value");
double d = [Link]();
[Link](d);
}
}
Example 7:boolean Demo
package userInput;
import [Link];
public class BooleanDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter boolean value");
boolean b = [Link]();
[Link](b);
}
}
Example 8:
package userInput;
import [Link];
public class CharDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter char value");
char c = [Link]().charAt(0);
[Link](c);
}
}
Example 9 : String Input
package userInput;
import [Link];
public class StringDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter String value");
String name = [Link]();
[Link](name);
}
}
Example 3: two no addition
package addition;
import [Link];
public class TwoNoAddition {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter first Number");
int a = [Link]();
[Link]("Enter Second Number");
int b=[Link]();
int c = a + b;
[Link](c);
}
}