0% found this document useful (0 votes)
14 views

Scanner class

The Scanner class in Java allows users to read various data types from input streams or files. It provides methods for reading integers, floats, doubles, and strings, with exceptions for invalid inputs. The document includes examples of using the Scanner class for reading user input and handling exceptions like FileNotFoundException.

Uploaded by

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

Scanner class

The Scanner class in Java allows users to read various data types from input streams or files. It provides methods for reading integers, floats, doubles, and strings, with exceptions for invalid inputs. The document includes examples of using the Scanner class for reading user input and handling exceptions like FileNotFoundException.

Uploaded by

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

Scanner class

The Scanner class is a class in java.util, which allows the user to read values of various types. There are far more methods
in class Scanner than you will need in this course. We only cover a small useful subset, ones that allow us to read in numeric
values from either the keyboard or file without having to convert them from strings and determine if there are more values to be
read.

Class Constructors

There are two constructors that are particularly useful: one takes an InputStream object as a
parameter and the other takes a FileReader object as a parameter.

Scanner in = new Scanner(System.in); // System.in is an InputStream


Scanner inFile = new Scanner(new FileReader("myFile"));

If the file ≥myFile≤ is not found, a FileNotFoundException is thrown. This is a checked exception, so it must be caught or
forwarded by putting the phrase ≥throws FileNotFoundException≤ on the header of the method in which the instantiation occurs
and the header of any method that calls the method in which the instantiation occurs.

Numeric and String Methods

Method Returns
int nextInt() Returns the next token as an int. If the next token is not an
integer,InputMismatchException is thrown.
long nextLong() Returns the next token as a long. If the next token is not an
integer,InputMismatchException is thrown.
float nextFloat() Returns the next token as a float. If the next token is not a
float or is out of range, InputMismatchException is
thrown.
double nextDouble() Returns the next token as a long. If the next token is not a float
or is out of range, InputMismatchException is thrown.
String next() Finds and returns the next complete token from this scanner and
returns it as a string; a token is usually ended by whitespace such
as a blank or line break. If not token
exists,NoSuchElementException is thrown.
String nextLine() Returns the rest of the current line, excluding any line separator
at the end.
void close() Closes the scanner.
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 . Although we have only shown four
numeric methods, each numeric data type has a corresponding method that reads values of that type.
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, given the following code segment and data

int number = in.nextInt();


float real = in.nextFloat();
long number2 = in.nextLong();
double real2 = in.nextDouble();
String string = in.next();
Eg:
import java.util.Scanner;
class ScannerTest{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);

System.out.println("Enter your rollno");


int rollno=sc.nextInt();
System.out.println("Enter your name");
String name=sc.next();
System.out.println("Enter your fee");
double fee=sc.nextDouble();
System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee);
sc.close();
}
}

import java.io.*;
import java.util.Scanner;
class AddTwoNumbers
{
public static void main(String args[])
{
int a, b, c;
System.out.println("Enter two integers to calculate the sum ");
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
c = a + b;
System.out.println("Sum of entered integers = "+c);
}
}

import java.io.*;
import java.util.Scanner;
public class MixedTypeInput
{
public static void main(String[] args)
{
double number;
Scanner in = new Scanner(System.in);
System.out.println("Enter your gross income: ");
if (in.hasNextInt())
{
number = (double)in.nextInt();
System.out.println("You entered " + number);
}
else if (in.hasNextFloat())
{
number = (double)in.nextFloat();
System.out.println("You entered " + number);
}
else if (in.hasNextDouble())
{
number = in.nextDouble();
System.out.println("You entered " + number);
}
else
System.out.println("Token not an integer or a real value.");
}
}

You might also like