Unit 4 sample programs

  1. Test File "zodiac.txt"
  2. Reading from a Text File
  3. StringTokenizer Class
  4. Manipulating Data
  5. Writing to a Text File
  6. Reading and Writing to a File


Illustrating DATAFILES zodiac.txt        

zodiac.txt contains 12x9 lines. Each group of 9 lines contain in order: Name of the house, dates, personality, colours, stones, metal, trees, flowers and the ruler of the house.
This file is used by several of the programs in this section and the next. Copy and save it on your floppy under the name: zodiac.txt



Illustrating Reading from a Text File: Ass4s2p2.java        

Write a program that will list the names of the house and its corresponding dates


// The "Ass4s2p2" class.
import java.awt.*;
import java.io.*;
import hsa.Console;
class Ass4s2p2
{
    static Console cs = new Console ();
    static FileReader f;
    static BufferedReader input;
    static String line;
    public static void main (String args []) throws IOException
    {
     f = new FileReader ("a:zodiac.txt");
     input = new BufferedReader (f);
     String house, dates;
     for (int record = 0 ; record < 12 ; record++)
     {
         house = input.readLine ();
         dates = input.readLine ();
         cs.print (house,11) ;
         cs.println( " reigns during the time period " + dates);
         // discard lines 3-9 of each record
         for (int i = 2 ; i < 9 ; i++)
          house = input.readLine ();
     }
     input.close ();
    } //end main
} //end Ass4s2p2




Illustrating StringTokenizer Class: Ass4s2p3.java        

Write a method String chopWords(String line, int num) which will insert the special character '\n' in the space every "num" words. The effect is, that when the line is printed, it will only print "num" words per line:

System.out.println(chopWords("Mary had a little lamb its fleece was white as snow",5))

would print out:

Mary had a little lamb
its fleece was white as
snow

Write a program that lets you input a line of words followed by a number, and that will format the line using chopWords().


// The "Ass4s2p3" class.
import java.awt.*;
import java.util.*;
import hsa.Console;
class Ass4s2p3
{
    static Console cs = new Console ();
    public static void main (String args [])
    {
     cs.println ("Type a short sentence to be chopped up :\n");
     String line = cs.readLine ();
     cs.print ("\nNow enter a number for the maximum number of words per line: ");
     int n = cs.readInt ();
     cs.println ("\n\n");
     String newLine = chopWords (line, n);
     cs.println (newLine);
    } //end main


    public static String chopWords (String paragraph, int num)
    {
     StringTokenizer x = new StringTokenizer (paragraph);
     String y = "";
     int count = 0;
     while (x.hasMoreTokens ())
     {
         count++;
         y = y + x.nextToken ();
         if (count == num)
         {
          y = y + "\n";
          count = 0;
         }
         else
          y = y + " ";
     }
     // the last character of y is needed:
     return y.substring (0, y.length () - 1);
    } //end chopWords
} //end Ass4s2p3



Illustrating Applets: Manipulating Data: Ass4s2p4.java        

Write a program that will let you enter a number, 1 -12 and that will print out the Zodiac name, its dates and the personality description, 8 words per line. Use chopWords().


// The "Ass4s2p4" class.
import java.awt.*;
import java.io.*;
import java.util.*;
import hsa.Console;
class Ass4s2p4
{
    static Console cs = new Console ();
    static String house [] = new String [12];
    static String dates [] = new String [12];
    static String personality [] = new String [12];
    public static void main (String args []) throws IOException
    {
     readFile ();
     cs.print ("enter a number from 1 to 12 (enter 0 to exit) ");
     // although users use numbers 1-12, the tables go 0-11
     int h = cs.readInt () - 1;
     while (h >= 0 && h < 12)
     {
         cs.clear ();
         cs.println ("you have chosen the house of " + house [h]);
         cs.println ("\nit reigns during " + dates [h] + "\n");
         cs.println (chopWords (personality [h], 8));
         cs.print ("\n\nenter a number from 1 to 12 (enter 0 to exit) ");
         h = cs.readInt () - 1;
     }
    } //end main

    //read data into the three array variables
    public static void readFile () throws IOException
    {
     String temp;
     FileReader f = new FileReader ("a:zodiac.txt");
     BufferedReader input = new BufferedReader (f);
     for (int record = 0 ; record < 12 ; record++)
     {
         house [record] = input.readLine ();
         dates [record] = input.readLine ();
         personality [record] = input.readLine ();
         // discard lines 3-9 of each record
         for (int i = 3 ; i < 9 ; i++)
          temp = input.readLine ();
     }
     input.close ();
    }

    public static String chopWords (String paragraph, int num)
    {
     StringTokenizer x = new StringTokenizer (paragraph);
     String y = "";
     int count = 0;
     while (x.hasMoreTokens ())
     {
         count++;
         y = y + x.nextToken ();
         if (count == num)
         {
          y = y + "\n";
          count = 0;
         }
         else
          y = y + " ";
     }
     // the last character of y is needed:
     return y.substring (0, y.length () - 1);
    } //end chopWords
} //end Ass4s2p4




Illustrating Writing to a Text File: Ass4s3p1.java        

Write a program that lets your enter 10 of your friends' names into the text file named a:friends.


// The "Ass4s3p1" class.
import java.awt.*;
import java.io.*;
import hsa.Console;
class Ass4s3p1
{
    static Console cs;
    static FileWriter f;
    static PrintWriter output;
    static String line;

    public static void main (String args []) throws IOException
    {
     cs = new Console ();
     f = new FileWriter ("a:friends.txt");
     output = new PrintWriter (f);
     for (int i = 1 ; i <= 10 ; i++)
     {
         cs.print ("enter the name of friend #" + i + " of 10: ");
         String name = cs.readLine ();
         output.println (name);
     }
     output.close ();
    } //end main
} //end Ass4s3p1



Illustrating Reading and Writing to a File: Ass4s3p4.java        

Write a program that will read all the data from the zodiac file and creates a new file, that will only contain the zodiac name, its dates and the ruler of the house.


// The "Ass4s3p4" class.
import java.awt.*;
import java.io.*;
import hsa.Console;
class Ass4s3p4
{
    static Console cs;
    static FileWriter fout;
    static FileReader fin;
    static PrintWriter output;
    static BufferedReader input;


    public static void main (String args []) throws IOException
    {
     cs = new Console ();
     fin = new FileReader ("a:zodiac.txt");
     fout = new FileWriter ("a:zodruler.txt");
     input = new BufferedReader (fin);
     output = new PrintWriter (fout);
     String house;
     String dates;
     String ruler;
     String temp;
     for (int i = 0 ; i < 12 ; i++)
     {
         house = input.readLine ();
         dates = input.readLine ();
         for (int j = 2 ; j < 8 ; j++)
          temp = input.readLine ();
         ruler = input.readLine ();
         output.println (house);
         output.println (dates);
         output.println (ruler);
     }
     output.close ();
     input.close ();
    } //end main
} //end Ass4s3p4






Sponsored by ECOO and SIG-Computer Science

.