Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; import java.util.ArrayList; /** Removes blank lines from a file */ public class DeleteBlankLines { /** Removes blank

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;

/**
Removes blank lines from a file
*/
public class DeleteBlankLines
{
   /**
Removes blank lines from the file given as argument and writes the non-blank lines
back to the file.
@param fileName the name of the file to be processed.
*/
public static void removeLines(String fileName)
{
// list to hold the non-blank lines
   ArrayList lines = new ArrayList();
 
//-----------Start below here. To do: approximate lines of code = 8
// In a try {..} block, create a File object using the fileName parameter,
   // then create a Scanner object passing in the File reference
   // then read the lines one by one. Check if the line string is non-blank by making use of the String class trim()
   // method. After trimming, check the size of the string to see if it is > 0 (or use isEmpty() method)
   // If it is > 0, add the line to the lines array list
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
 
//-----------Start below here. To do: approximate lines of code = 7
// In a try {..} block, create a file for writing using the PrintWriter class and the same parameter fileName
   // as above. Then output the lines in the array list to this file. Don't forget to close the file!!
 
 
 
 
 
 
 
 
 
 
 
 
 
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}

public static void main(String[] args)
{
removeLines("lines.txt");
int count = 0;
try (Scanner in = new Scanner(new File("lines.txt")))
{
while (in.hasNextLine())
{
in.nextLine();
count++;
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
System.out.println("Number of non-blank lines: " + count);
System.out.println("Expected:Number of non-blank lines: 3");
}
}


Step by Step Solution

3.41 Rating (154 Votes )

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Document Format ( 2 attachments)

PDF file Icon
608c00a376f23_208013.pdf

180 KBs PDF File

Word file Icon
608c00a376f23_208013.docx

120 KBs Word File

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Discovering Advanced Algebra An Investigative Approach

Authors: Jerald Murdock, Ellen Kamischke, Eric Kamischke

1st edition

1559539844, 978-1604400069, 1604400064, 978-1559539845

More Books

Students also viewed these Chemical Engineering questions

Question

What is FDI all about? Put it in your own words.

Answered: 1 week ago

Question

4. How does a sex-linked gene differ from a sex-limited genepg99

Answered: 1 week ago