PastYearFile(solution).doc - EXERCISE(FILE-solution PAST...

This preview shows page 1 - 3 out of 6 pages.

EXERCISE (FILE)-solution PAST YEAR QUESTION June 2015 //Reading using token method import java.util.*; import java.io.*; import java.util.StringTokenizer; public class Destination { public static void main (String[] args) throws IOException { File fileIn = new File ("Flight.txt"); File fileOut = new File ("Indonesia.txt"); try{ Scanner in = new Scanner (fileIn); PrintWriter out = new PrintWriter (fileOut); String code, name, dest; int numAdult, numChild; int count = 0; int totAdult = 0; int totChild = 0; String indata = null; while (in.hasNext()) { indata = in.nextLine(); StringTokenizer st = new StringTokenizer(indata, "/"); code = st.nextToken(); name = st.nextToken(); dest = st.nextToken(); numAdult = Integer.parseInt(st.nextToken()); numChild = Integer.parseInt(st.nextToken()); if (name.equalsIgnoreCase("Ahmad Hasif")) System.out.println (code + " " + name +" " + dest + " " + numAdult + " " + numChild + " "); if (dest.indexOf("[Indonesia]") != -1) { count++; out.println (code + " " + name +" " + dest + " " + numAdult + " " + numChild + " "); totAdult += numAdult; totChild += numChild; } }
out.println ("Average of adults per flight " + (totAdult / count)); out.println ("Average of child per flight " + (totChild / count)); in.close(); out.close(); } catch (FileNotFoundException fnf) { System.out.println (fnf.getMessage());

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture