Write a program (using scanner class) to input a line and display only numeric tokens.
Sample Input : 26 January 1950 is celebrated as Republic day
Sample output : 26 1950
Answers
//To display only numeric tokens present in a line
import java.util.Scanner;
public class NumToken
{
//main method to test Scanner Class
public static void main(String[] args)
{
System.out.println("Enter line ending with a space & terminate with (.) :");
Scanner in=new Scanner(Sytem.in);
while(true)
{
if(in.hasNextInt());
System.out.print(in.nextInt()+" ");
if(in.next().equals("."))
break;
}
}
}
Explanation:
import java.util.*;
public class Token
{
public status void main (String () args)
{
System.out.Printing("Enter line ending with a space & terminating with (.) :");
Scanner sc = new Scanner(System.In);
while(true)
{
if(in.hasNextInt())
System.out.Print on(in.nextInt()+" ");
if(in.next().equals("."))
break;
}
}
}