<tutorial title="Reading a tab delimited file" ht='1'>
 <blurb>
  <p>
  A tab delimited file has a simple structure. Every line (record)
  has the same number of fields. Each field is separated by the tab
  character.
  </p>
  <p>Each line of the file <code><a href="cia.txt" target="doc">cia.txt</a>
  </code> represents a country. Each has five fields:
  </p>
  <ul>
   <li>name</li>
   <li>region</li>
   <li>area</li>
   <li>population</li>
   <li>gdp</li>
  </ul>
  <p>A sample from cia.txt:</p>
  <pre>Lithuania	Europe	65200	3596617	45230000000
Luxembourg	Europe	2586	468571	27270000000
</pre>
 </blurb>
 <question className="Demo" title="Splitting the fields" copyFile='cia.txt'>
  <blurb>
  <p>
  We split the line on the tab character and copy each field into
  a variable with an appropriate name and an appropriate type.
  </p>
  <p>In a tab delimited file we need to convert each field from a String
  to the appropriate type if required.</p>
  <p class='imper'>
  This program prints the name for every country with a large population.
  </p>
  </blurb>
  <shell lang="java" className="Demo" import="shells.xml#null"/>
<prog lang="java"><![CDATA[
import java.io.BufferedReader;
import java.io.FileReader;
public class Demo{
  public static void main(String[] argv)
                        throws Exception{
    BufferedReader fh =
     new BufferedReader(new FileReader("cia.txt"));
    String s;
    while ((s=fh.readLine())!=null){
      String f[] = s.split("\t");
      String name = f[0];
      String region = f[1];
      int area = Integer.parseInt(f[2]);
      int population = Integer.parseInt(f[3]);
      long gdp = Long.parseLong(f[4]);
      if (population>200000000)
      {
        System.out.println(name);
      }
    }
    fh.close();
  }
}
]]></prog>
<prog lang="cs"><![CDATA[
using System;
using System.IO;
public class Demo{
  public static void Main(string[] argv)
  {
    StreamReader fh = new StreamReader("cia.txt");
    string s;
    while ((s=fh.ReadLine())!=null){
      string[] f = s.Split('\t');
      string name = f[0];
      string region = f[1];
      int area = Convert.ToInt32(f[2]);
      long pop = Convert.ToInt32(f[3]);
      long gdp = Convert.ToInt64(f[4]);
      if (pop>200000000)
      {
        Console.WriteLine(name);
      }
    }
    fh.Close();
  }
}
]]></prog>
 </question>
</tutorial>

