Sunday, 26 April 2015

Filled Under:

How To Take Input From User Using Scanner In Java

This tutorial shows how to take input at run time in java using util.Scanner class in java and store in thier respective types as shown in this program.

SCANNER IN JAVA:

A text scanner can parse all primitives and string using regular expression.First it breaks it pattern into delimiter pattern then these resulting patterns may then be converted into different data types using various next methods.

Next METHOD In JAVA:

This method is used to find token from the scanner.This method can be blocked during waiting for input to scan.
IT THROWS:

NOSuchElementException: If  it has no more token.
IllegalStateException:If  that scanner is closed.


CODING:

import java.util.Scanner;
class scanner     
{
public static void main(String args[])
{
String str;
float fl;
int no;
char ch;
Scanner input=new Scanner(System.in);
System.out.println("ENTER ANY STRING");
str =input.nextLine();
System.out.println("ENTER ANY FLOAT VALUE");
fl =input.nextFloat();
System.out.println("ENTER ANY NUMBER");
no =input.nextInt();
System.out.println("ENTER ANY CHARACTER");
ch =input.next().charAt(0);
System.out.println("INPUT STRING="+str);
System.out.println("INPUT CHARACTER="+ch);
System.out.println("INPUT NUMBER="+no);
System.out.println("INPUT FLOAT NUMBER="+fl);
}
}

OUTPUT:


0 comments:

Post a Comment