Guest User

Untitled

a guest
Feb 7th, 2010
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. /*
  2. Program Description:
  3.  
  4. Program takes a line of text from a text file (excerpt) and outputs it on the console. The program also prints the
  5. entire text file from which the excerpt was taken. The name of the text file to be used is to be inputted as a command line argument.
  6.  
  7. */
  8.  
  9. import java.io.*;
  10. import java.util.Random;
  11. import java.io.FileReader;
  12. import java.io.File;
  13.  
  14. public class Main
  15.  
  16. {
  17.     BufferedReader reader = null;
  18.     File filename = null;
  19.     String line;
  20.     LineNumberReader lnr = null;
  21.     FileReader fr = null;
  22.    
  23.     int numOfLines=0;
  24.    
  25.    
  26.     public Main (String nameOfTextFile)
  27.    
  28.     {
  29.        
  30.         try
  31.        
  32.         {
  33.             filename = new File(nameOfTextFile);
  34.             reader = new BufferedReader(new FileReader(filename));
  35.             fr = new FileReader(filename);
  36.             lnr = new LineNumberReader(fr);
  37.            
  38.         }
  39.        
  40.         catch(FileNotFoundException e)
  41.         {
  42.             System.out.println("Error: text file does not exist");
  43.         }
  44.        
  45.         catch(Exception e)
  46.         {
  47.             System.out.println("General Errors");
  48.         }
  49.        
  50.        
  51.     }
  52.    
  53.     public void printAllLines ()
  54.    
  55.     {
  56.        
  57.         try
  58.         {
  59.            
  60.             line = reader.readLine();
  61.            
  62.             while (line != null)
  63.             {
  64.                 System.out.println(line);
  65.                 line = reader.readLine();
  66.             }
  67.            
  68.             reader.close ();
  69.         }
  70.        
  71.         catch(Exception e)
  72.         {
  73.             System.out.println("Error: file manipulation error");
  74.         }
  75.        
  76.        
  77.     }
  78.    
  79.    
  80.     public void getExcerpt ()
  81.    
  82.     {
  83.        
  84.         try
  85.         {
  86.            
  87.             int startingLineNum=0;
  88.             int endingLineNum=0;
  89.             int lineCount=0;
  90.             Random rand = new Random();
  91.            
  92.             line = reader.readLine();
  93.            
  94.             while (line != null)
  95.             {
  96.                
  97.                 System.out.println(line);
  98.                 line = reader.readLine();
  99.                 numOfLines ++;
  100.             }
  101.            
  102.            
  103.            
  104.             while (line != null)
  105.             {
  106.                
  107.                 System.out.println(line);
  108.                 line = reader.readLine();
  109.                 numOfLines ++;
  110.             }
  111.            
  112.            
  113.            
  114.             startingLineNum = rand.nextInt(numOfLines);
  115.             endingLineNum = rand.nextInt(numOfLines);
  116.            
  117.            
  118.         }
  119.        
  120.         catch(Exception e)
  121.         {
  122.             System.out.println("Error: file manipulation error");
  123.         }
  124.        
  125.         finally
  126.         {
  127.            
  128.         }
  129.     }
  130.    
  131.    
  132.     public static void main(String[] args) throws Exception
  133.    
  134.     {
  135.        
  136.         try
  137.         {
  138.            
  139.             Main excerptDemo = new Main (args[0]);
  140.             System.out.println ("\nText File Excerpt:\n");
  141.             excerptDemo.getExcerpt ();
  142.            
  143.             System.out.println ("\nExcerpt taken from: \"" +excerptDemo.filename.getName() + "\"\n");
  144.             excerptDemo.printAllLines ();
  145.            
  146.             //the two bolded methods are the methods that can't seem to work together
  147.            
  148.         }
  149.        
  150.         catch (ArrayIndexOutOfBoundsException e)
  151.         {
  152.             System.out.println("Error: Name of text file was not specified in command line argument");
  153.         }
  154.        
  155.     }
  156.    
  157.    
  158.    
  159.    
  160. }
Advertisement
Add Comment
Please, Sign In to add comment