import java.io.*;
import java.util.*;

// Introduciton to Java I/O

public class JavaInput {
    
    public static void main(String[] args) throws Exception {
        // Generally, there are two ways of reading input in Java
        
        /*
         * Before java 1.5, usually we read input line by line
         * and the rest is string processing.
         */
	
        // To read from standard in:
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        // To read from file:
        BufferedReader fin = new BufferedReader(new FileReader("myfile"));
        // To read from string:
        BufferedReader sin = new BufferedReader(new StringReader("read this string"));

        /*
         * After reading in the lines, we parse it.
         * We will use standard input for example.
         */
        
        String line;
        while( (line = cin.readLine()) != null) { // this checks if there are more input
            
            // to get rid of leading or trailing whitespaces
            // quite useful, in case some extra space was in the file by accident
            line = line.trim();
            
            // suppose we have an integer
            int myInt0 = Integer.parseInt(line);
            
            // suppose we have a binary integer
            int myInt1 = Integer.parseInt(line, 2);
            
            // suppose we have a double, and so on....
            double myDbl = Double.parseDouble(line);
            
            // suppose we have a whole bunch of space delimited integers
            // e.g. "1 4 2 3 5"
            
            String[] toks = line.split(" +"); // " +" is a regular expression
            int[] myInts = new int[toks.length];
            for(int i = 0; i < toks.length; i++)
                myInts[i] = Integer.parseInt(toks[i]);
            
            // read up the API of split to see its behavior and options
            // when there are trailing, leading, multiple delimiters
        }
        
        /*
         * Now, with Java 1.5, we use Scanner
         * Scanner automatically deals with whitespaces
         * (similar to cin in C++)
         */
        
        Scanner in;
        // here is scanner from standard in, string, and file
        in = new Scanner(System.in);
        in = new Scanner("scan this string");
        in = new Scanner(new File("myFile"));
        
        // you can change the deliminters to something other than
        // whitespace by passing in a second argument
        
        // e.g. read a bunch of ints
        while(in.hasNextInt()) {
            int garbage = in.nextInt();
        }
 
        // similarily, there is nextDouble(), nextBigInteger(), etc.
        
        // sometimes we still need to do line by line processing
        // e.g. when you want to sum up (space delimited) integers in a line,
        //      but you don't know how many integers are in the line
        
        String lineOfInt = in.nextLine();
        String[] moreToks = lineOfInt.trim().split(" +");
        int sum = 0;
        for(String s : moreToks) sum += Integer.parseInt(s);
        
        // for even more nasty things, check out details of split,
        // as well as the String API
        
        
        /*
         * WARNING:
         * Take extreme care when switching between nextInt() and nextLine()
         * e.g. If input is:
         * 1
         * 1 2 3 4 5
         * 
         * after readInt()
         * 
         * 1
         *  ^
         * 1 2 3 4 5
         * 
         * the next readLine() gives an empty line, and moves the caret to next line
         * 
         * 1
         * 1 2 3 4 5
         * ^
         * 
         * now, the second readLine() will give you the meaningful stuff
         */
    }

}
