Scanner class in java



Scanner

Java.util.Scanner class provides convenient methods to read data in the form of primitive type as well as string from the keyboard.

public Scanner(String source)

public Scanner(File source)

public Scanner(InputStream source)

public int nextInt(int radix)

public String nextLine()

public float nextFloat()

public static void main(String as[])throws IOException{

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter Roll");

    int a=sc.nextInt();

    System.out.println(a);

}

Scanner is also used for Splitting tokens: From above example

public class RecorderReadUsingScanner {

public static void main(String as[])throws Exception{

    BufferedReader b=new BufferedReader(new InputStreamReader(new FileInputStream("d:/Emp.txt")));

  

  

    Emp e;

    while(true)

    {

       String str=b.readLine();

       if(str==null)

           break;

       Scanner sc=new Scanner(str);

       sc.useDelimiter("\t");

       String name= sc.next();

       int marks=Integer.parseInt(sc.next());

       String address=sc.next();

       e=new Emp(name, marks, address);

       e.display();

    }

}

No comments:

Post a Comment