[go: up one dir, main page]

0% found this document useful (0 votes)
26 views4 pages

Scanner Class

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

Scanner Class

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

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(System.in);

2) Import scannner class from java.util package.

import java.util.Scanner;

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 java.util.Scanner;

public class ByteDemo {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.println("Enter Byte Value");
byte b = sc.nextByte();
System.out.println(b);
}
}

Example 2 : short type input

package userInput;

import java.util.Scanner;

public class ShortDemo {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter Short value");
short s = sc.nextShort();
System.out.println(s);
}
}

Example 3 :int input

package userInput;

import java.util.Scanner;

public class IntDemo {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.println("Enter Int No");
int no = sc.nextInt();
System.out.println(no);

}
}

Example 4: long input

package userInput;

import java.util.Scanner;

public class LongDemo {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter Long Number");
long l = sc.nextLong();
System.out.println(l);

}
}

Example 5: float input

package userInput;

import java.util.Scanner;

public class FloatDemo {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.println("Enter float No");
float no = sc.nextFloat();
System.out.println(no);
}
}

example 6: double input

package userInput;

import java.util.Scanner;

public class DoubleDemo {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.println("Enter Double value");
double d = sc.nextDouble();
System.out.println(d);

}
}

Example 7:boolean Demo

package userInput;

import java.util.Scanner;

public class BooleanDemo {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.println("Enter boolean value");
boolean b = sc.nextBoolean();
System.out.println(b);
}
}

Example 8:

package userInput;

import java.util.Scanner;

public class CharDemo {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.println("Enter char value");
char c = sc.next().charAt(0);
System.out.println(c);
}

}
Example 9 : String Input

package userInput;

import java.util.Scanner;

public class StringDemo {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.println("Enter String value");
String name = sc.next();
System.out.println(name);
}
}

Example 3: two no addition

package addition;

import java.util.Scanner;

public class TwoNoAddition {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);


System.out.println("Enter first Number");
int a = sc.nextInt();
System.out.println("Enter Second Number");
int b=sc.nextInt();

int c = a + b;
System.out.println(c);
}
}

You might also like