#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    I am getting an error on this


    Specific functional requirements for this project:
    1. The application should ask the user for the name of input and output files.

    2. The application should open the file and read each line of the file.

    3. For each line read, the application should determine the total number of characters in the line (including white-space characters), determine the number of times the number 4 appears in the line, replace all occurrences of the string "this" with "That", and reverse the order of the characters in the line.

    4. The output file should contain the same number of lines as the input file with the following content for each line:
    a. Total number of characters (including white-space characters) in the line
    b. Number of times the number 4 appeared in the line
    c. Reversed string (for example, cats in the input file would be displayed as stac)


    The items on each line in the output file should be separated by commas (for example: for input line "this4cats", the output line should be "9,1,stac4tahT")

    5. The application should close any open files before exiting.


    I have done this I am getting an error "Exception in thread "main" java.lang.NullPointerException
    at HW3.main(HW3.java:80)"


    Code:
    // HW3.java
    
    /*
    *Alogorithm:
      Ask the user for the name of input and output files. 
      Open the input file and read each line of the file.
      Open output file
    For each line read:
        - determine the total number of characters in the line
        - determine the number of times the number 4 appears in the line,
        - replace all occurrences of the string "this" with "That", \
        - reverse the order of the characters in the line.
      Write to the output file the following content for each line seperated by comas:
        - Total number of characters (including white-space characters) in the line
        - Number of times the number 4 appeared in the line
        - Reverse the line
    Close files before exiting.
    */
    
    import java.io.*;
    import java.util.Scanner;
    
    public class HW3 {
       
    
      public static void main( String[] args ) {
         
          // input and output file names
          String inputFile, outputFile;
    
          // print program title
          System.out.println("HW3\n");
       
        // make keyboard reader
        Scanner kybd = new Scanner(System.in);
     
       
      // get input and output file names
          System.out.print("Enter input file name: ");
          inputFile = kybd.nextLine();
          System.out.print("Enter output file name: ");
          outputFile = kybd.nextLine();
    
     
      // open input file
          Scanner fin = null;
         
          try{
         
            fin = new Scanner(new File(inputFile));
          }
         
          catch(FileNotFoundException ex)
          {
              System.out.println("Cannot open input file: " + inputFile);
          }
           
           
          // open output file
          PrintWriter fout = null;
         
          try{
         
            fout = new PrintWriter(new FileWriter(outputFile));
     
          }
          catch(FileNotFoundException ex)
          {
              System.out.println("Cannot open output file: " + outputFile);
          }
               
          catch(IOException ex)
          {
              System.out.println("Cannot write to output file: " + outputFile);
          }
         
     
          // read each line of the file.
        while(fin.hasNext())
        {
       
        String line = fin.nextLine();// read line from file
       
        System.out.println(line); // echo to screen
       
        // use Stringbuilder
        StringBuilder sb = new StringBuilder(line);
       
        int numChars = sb.length(); // characters in the line
       
        // count number of times the number 4 appears in the line,
        int count = 0;
       
        // for length of line
        for(int i=0;i<sb.length();i++)
        {
            if(sb.charAt(i) == '4')
                count++;
        }
       
        // replace all occurrences of the string "this" with "That",
        int index = sb.indexOf("this");
       
        // while 'that' found
        while(index>=0)
        {
        sb.replace(index,index+4,"That"); // replace 'this' with 'That'
        index = sb.indexOf("this"); // fund next 'this'
        }
       
        // reverse the order of the characters in the line.
        sb.reverse();
     
        // write results to output file and screen
        fout.println(numChars + "," + count + "," + sb.toString());
        System.out.println(numChars + "," + count + "," + sb.toString() + "\n");
      } // end while
    
        // close files
    
        fin.close();
        fout.close();   
       
      }
    
    
    }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    I ran your code, but didn't get an error.

IMN logo majestic logo threadwatch logo seochat tools logo