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 |