Showing posts with label Scanner Class. Show all posts
Showing posts with label Scanner Class. Show all posts

Tuesday, October 25, 2011

BlueJ Program On String Using Scanner Class


program to enter a string using scanner and find out the number of words,spaces,letters,numbers and special characters in it.


import java.util.Scanner;
public class Scan
{
  public static void main(String[] args)
  {
    Scanner sc = new Scanner(System.in);
    String s1;
    int i,len,space=0,letter=0,digit=0,punc=0;
    char ch;
   System.out.println("Enter a sentence:");
    s1 = sc.nextLine();
    len=s1.length();
    for(i=0;i< len;i++)
    {
         ch=s1.charAt(i);
         if(ch==' ')
         space++;
         else if((ch>=65 && ch< =90) ||(ch>=97 && ch< =122))
         letter++;
         else if(ch>=48 && ch< =57)
         digit++;
         else
         punc++;
        }

       System.out.println("Number of Words in the sentence="+(space+1));
       System.out.println("Number of spaces in the sentence="+space);  
       System.out.println("Number of letters in the sentence="+letter);
       System.out.println("Number of digits="+digit);
       System.out.println("Number of punctuations="+punc);
    
    }

}

Sample input and output:

Enter a sentence:
This is our first test
Number of Words in the sentence=5
Number of spaces in the sentence=4
Number of letters in the sentence=18
Number of digits=0
Number of punctuations=0

Enter a sentence:
Address is: 14,circular road
Number of Words in the sentence=4
Number of spaces in the sentence=3
Number of letters in the sentence=21
Number of digits=2
Number of punctuations=2

Related Post: BlueJ Programs on String/Sentence

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


Monday, May 9, 2011

Simple BlueJ program using Scanner class

This is a simple program to demonstrate the use of Scanner class for taking user input
Scanner is a class available in java.util package. The Scanner class is imported to our program using the import statement. This is a simple program to enter three numbers from the console and the sum of the numbers will be displayed. Firstly Scanner class object is created using the statement ‘Scanner input = new Scanner(System.in);’. Constructor of Scanner class takes ‘System.in’ as argument. ‘nextInt ()’ function of Scanner class is used to retrieve values.
  
import java.util.Scanner;
public class Inp
 {
   public static void main(String args[])
    {
          Scanner input = new Scanner(System.in);
          int num1, num2, num3, sum;
          System.out.print(" Enter 1st Number: ");
            num1 = input.nextInt();  
          System.out.print(" Enter 2nd Number: ");
            num2 = input.nextInt();
           System.out.print(" Enter 3rd Number: ");
            num3 = input.nextInt();
           sum = num1 + num2 + num3;
          System.out.println("sum= " + sum);
         }
 }

Subscribe via email

Enter your email address:

Delivered by FeedBurner