Tuesday, October 25, 2011

Use of Scanner class in BlueJ programs


The Scanner class is available in java.util package which allows users to enter values in specific format. Normally we use BufferedReader class of java.lang package with permits to enter values either in string or int (readLine () function of BufferedReader class enters values in string only which need to be converted into respective type using static parsing functions of Wrapper class defined in java.lang package. The other function of BufferedReader class is ‘read ()’ which enters a single character and returns its ASCII value).

Different functions defined in Scanner class
int nextInt(): Returns the next token as an int value. If the next token is not an integer value, InputMismatchException object is thrown.

long nextLong():Returns the next token as a long value. If the next token is not an integer, InputMismatchException object is thrown.

float nextFloat(): Returns the next token as a float value. If the next token is not a float value, InputMismatchException object is thrown.

double nextDouble():Returns the next token as a double value. If the next token is not a float value, InputMismatchException object is thrown.

String next():Returns the next token as a string value. A token is usually terminated by whitespace, means single word can be entered using this function. If there is no token, NoSuchElementException object is thrown by this function. One point to be noted here that any type of values can entered as string.

String nextLine():Returns the entire input as string.

void close (): closes the Scanner class object.

Scanner class object accept values as tokens. Tokens are sequence of values which is terminated by blank spaces. Unlike readLine () or read () functions of BufferedReader class, Scanner class object can accept number of values at a time. The values are to be entered separated with space.

import java.util.Scanner;
public class Scan
{
  public static void main(String[] args)
  {
    Scanner sc = new Scanner(System.in);
    int a;
    long b;
   double c;
    String s1,s2;
   System.out.println("Enter an integer, a long  and a double value ( Each values should be separated by a space):");
    a = sc.nextInt();
    b = sc.nextLong();
    c = sc.nextDouble();
    System.out.println("The entered values are:"+a+","+b+","+c);
    System.out.println("Enter a word.");
    s2 = sc.next();
    System.out.println("Entered word="+s2);
  }

}


Sample input and output:

Enter an integer, a long and a double value ( Each values should be separated by a space):
1 2 3
The entered values are:1,2,3.0
Enter a word.
wwwww
Entered word=wwwww

Enter an integer, a long and a double value ( Each values should be separated by a space):
33 5555555 66.7
The entered values are:33,5555555,66.7
Enter a word.
aaa bbb ccc
Entered word=aaa

Another program using Scanner class to enter any sentence:

import java.util.Scanner;
public class Scan
{
  public static void main(String[] args)
  {
    Scanner sc = new Scanner(System.in);
    String s1;
   System.out.println("Enter a sentence:");
    s1 = sc.nextLine();
    System.out.println("Entered sentence="+s1);
    }

}

Sample input and output:

Enter a sentence:
this is another test
Entered sentence=this is another test


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner