Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

adplus-dvertising
User Input in Java using Scanner Class
Previous Home Next

Java 1.5 introduced the Scanner class which simplifies console input. It can also be used to read from files and Strings (among other sources). The Scanner class in Java is used for taking input from the user. The Scanner class can take input of all the data types. This class is present in java.util package. Scanner class in java is a widely use pre-defined function by the Java programmers. It has many sets of methods but it is commonly use to read the input declared on different data types like int, double, and string. The input data must be delimited by some character. By default the delimiters are white space (space, tabs, and new lines). The class provides methods for changing the delimiter.

Scanner is a member of the java.util package and must be imported into your class in order to make it available for use. Import statements are coded at the top of your program:

The Scanner class provides a variety of constructors that accept a data source as a parameter. Some of the most useful constructors include:

Scanner(InputStream), Scanner(File), Scanner(String).

Here are some constructor calls that builds Scanner objects for various input sources:
     //Build a Scanner that reads a String object
     Scanner sc1 = new Scanner("This is the input string");
     //Build a Scanner that reads the keyboard
     Scanner sc2 = new Scanner(System.in);
     //Build a Scanner that reads a file
     Scanner sc3 = new Scanner(new File("c:\\java\\data.txt"));    

The Scanner looks for tokens in the input. A token is a series of characters that ends with what Java calls whitespace. A whitespace character can be a blank, a tab character, a carriage return, or the end of the file. Thus, if we read a line that has a series of numbers separated by blanks, the scanner will take each number as a separate token. The numeric values may all be on one line with blanks between each value or may be on separate lines. Whitespace characters (blanks or carriage returns) act as separators. The next method returns the next input value as a string, regardless of what is keyed. For example:

int number = in.nextInt();
float real = in.nextFloat();
long number2 = in.nextLong();
double real2 = in.nextDouble();
String string = in.next();
Method Description
public String next()it returns the next token from the scanner.
public String nextLine()it moves the scanner position to the next line and returns the value as a string
public byte nextByte()it scans the next token as a byte.
public short nextShort()it scans the next token as a short value.
public int nextInt()it scans the next token as an int value.
public long nextLong()it scans the next token as a long value.
public float nextFloat()it scans the next token as a float value.
public double nextDouble()it scans the next token as a double value.

Example :

import java.util.Scanner;
class Sum
{
	public static void main(String args[])
	{
		Scanner sc;
		sc=new Scanner(System.in);
		int a,b,c;
		System.out.print("pls enter first number :");
		a=sc.nextInt();
		System.out.print("pls enter second number :");
		b=sc.nextInt();
		c=a+b;
		System.out.print("sum is "+c);
	}
}
Previous Home Next