Gregory Desrosiers Montreal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 31 May 2013

JAVA Program: Command-based String Remover in Text Files

Posted on 20:37 by Unknown
Hello, everyone.

Let me share with you the code on one of the programs I did back in March 2013, while doing my Data Structures class. This is a command-based program where what you do is enter what string you want to remove from a text file, than which text file you want to modify and return its result.

There is some documentation at the top of the code so that way you guys can understand how this works. The modified text file is always saved as ResultFile.txt.

The way on how to run it perfectly is to go onto the Microsoft Command Prompt, go to the directory where the class file is held, making sure all of the JAVA resources are there, then type in the following:

java Chapter14Exercise11 abc HelloThere.txt

"abc" is the string to be removed from the text file "HelloThere.txt."

/*
 *     Data Structures and Object-Oriented Programming - Assignment 6
 *     Question 14.11 of Introduction to Java Programming (by Daniel Liang)
 *
 *     Text Remover (Command Prompt fashion)
 *
 *     This program opens up an existing file and removes a string, both
 *     specified through the console that originally calls and executes
 *     the program. The resulting file is ResultFile.txt; if the Scanner
 *     object and the PrintWriter object refer to the same File object,
 *     the result will be a full blank file with zero bytes stored in memory.
 *
 *     Programmed by Gregory Desrosiers
 *
 *     Start Date: March 10, 2013
 *     End Date: March 10, 2013
 *
 *     File Name: Chapter14Exercise11.java
 *     Teacher: Amin Ranj Bar
 *
 *     *NOTE*: All strings to be removed, including single characters, are
 *     case-sensitive.
 *
 *     Small Change made on March 13, 2013: The "throws IOException" clause after the
 *     brackets in the header of the main has been removed because IOException is a
 *     subclass of Exception and is being handled by the try-catch-finally block.
 *
 */

import java.util.Scanner;
import java.io.*;

public class Chapter14Exercise11
{
    public static void main(String[] args)
    {
        boolean programComplete = false;
      
        Scanner fileReader = null;
        PrintWriter fileWriter = null;
      
        if(args.length == 0)
        {
            System.out.print("You have not specified what string to remove in the file and what file to access.\n\n" +
                    "Please execute program with the desired associate commands.");
            quit();
        }
        else if (args.length == 1)
        {
            System.out.print("What file do you want the program to access and remove the string?\nYou have not specified the file.\n\nPlease enter file name.");
            quit();
        }
        else if (args.length == 2 && !args[1].contains("."))
        {
            System.out.print("The file you specified does not contain a period. You need to enter the file extension.\n\n");
            quit();
        }
      
        try
        {
      
            fileReader = new Scanner(new File(args[1]));
            fileWriter = new PrintWriter(new File("ResultFile.txt"));
      
            while(fileReader.hasNext())
            {
                String currentLine = fileReader.nextLine();
          
                fileWriter.println(currentLine.replaceAll(args[0], ""));
            }
          
            System.out.print("File Operation complete. ");
            programComplete = true;
        }
        catch (Exception error)
        {
            if (error instanceof FileNotFoundException)
                System.err.println("Sorry! The file has not been found.");
            else if (error instanceof IOException)
                System.err.println("Something went wrong with either writing or reading the " +
                        "specified files. Needs to close.");
          
            // Debugging purpose only
            else if (error instanceof ClassNotFoundException)
            {
                System.err.println("Required classes have not been found.");
                error.printStackTrace();
            }
        }
        finally
        {
            if (fileReader != null)
                fileReader.close();
          
            if (fileWriter != null)
                fileWriter.close();
          
            if (programComplete)
                System.out.println("Program complete.");
        }
    }
  
    public static void quit()
    {
        System.out.println("Program terminated.");
        System.exit(1);
    }
}


That does it for this code! Have a nice night!
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • The Quebec Ministerial Exam of College English (English Exit Exam)
    Hello, everyone. How you doing? I'm good. There is some unfortunate news for me to deliver, though I am planning to try to work myself a...
  • Computer Game Collection
    Hello, people! Before I happen to leave this very foundation of Champlain College Saint-Lambert (that's where I'm learning to get wh...
  • String Letter Counter (JAVA Program)
    Hello, guys. This is another JAVA Program I want to share with you guys. It's a counter for a specific letter in an entered string strai...
  • JAVA Program: Command-based String Remover in Text Files
    Hello, everyone. Let me share with you the code on one of the programs I did back in March 2013, while doing my Data Structures class. This ...
  • The Black Glove [Short Poem by me]
    This is a poem written by me back in April 2010. I'm not sure if this was for an assignment, but this is what I got. It may not be good ...
  • DOS Games to Play
    Hello, people. If there's one thing that I want to do when I have lots of money, it's to play full version DOS Games on my Windows P...
  • Fundraising Project (To Autism Society Canada)
    Hi, everyone! How are you people doing? I am feeling tired and stuff, but there are a couple of things I want to bring up with you. Tomorrow...
  • Princess Peach In India [Story Idea]
    Here's a really fancy story I have made, although it is more of an initial idea and comes from a visual I had more than two years ago. E...
  • Best VGM Series
    Hello, guys. I want to present to you a series of YouTube imports of video game music, with some of them already recorded but in a form that...
  • Super Mario Script 1 (v 2.3) [Super Mario Galaxy font, with a few photos]
    Hello there. Do you recognize the font of this text? I'll give you two clues. The one below is the a sample sentence of different sizes....

Blog Archive

  • ▼  2013 (38)
    • ►  August (2)
    • ►  July (9)
    • ►  June (12)
    • ▼  May (12)
      • JAVA Program: Number Loop (Experimental Program of...
      • JAVA Program: Displaying Unlimited Strings on a GU...
      • JAVA Program: Command-based String Remover in Text...
      • Arcade Games to check out
      • JAVA Program: Random 0s and 1s Table with Refresh ...
      • JAVA Program: Object-Oriented Programming Class, t...
      • Announcement on "The Asperger Computer"
      • JAVA Program : Hello World!
      • Introducing myself (original blog on Autism Suppor...
      • Demonstration of JAVA Classes
      • Non-fiction e-book, The Asperger Computer, reveals...
      • The Asperger Computer - Facebook Edition
    • ►  April (2)
    • ►  January (1)
  • ►  2012 (24)
    • ►  December (13)
    • ►  November (3)
    • ►  October (5)
    • ►  September (1)
    • ►  August (2)
Powered by Blogger.

About Me

Unknown
View my complete profile