My teacher want us to type up a program but i really dont have any idea how to s
ID: 3544538 • Letter: M
Question
My teacher want us to type up a program but i really dont have any idea how to start this program, need someone to help me. PLZ
here is the assignment:
Use Chooser to find a file to read. Your program should open it, and create a new file in the same folder with the same name but ".txt" appended to the end of the name. For example, if the user selects: "Z:JavaTestFile.txt", your program will create a file named "Z:JavaTestFile.txt.txt",
If the user cancels instead of actually selecting a file, no output file should be created, and the user should get an acknowledgement message.
You can use PrintWriter to write, and BufferedReader to read. Assume ASCII files.
Replace every third instance of "AArdvark" with "Dodo" in the file. Searches will be non-case sensitive, but always replace with "Dodo". Replace the first, fourth, seventh, etc. instance.
Sample input file:
Mary had a little aardvark, and another AARDVARK.
Mary liked aardvarks,
but her sister didn't
Mary had a little aardvark, and another AARDVARK.
Mary liked aardvarks, really liked aardvarks.
but her sister didn't
Mary had a little aardvark, and another AARDVARK.
Mary liked aardvarks,
but her sister didn't
Expected output file:
Mary had a little Dodo, and another AARDVARK.
Mary liked aardvarks, really liked Dodos.
but her sister didn't
Mary had a little aardvark, and another AARDVARK.
Mary liked Dodos,
but her sister didn't
Mary had a little aardvark, and another AARDVARK.
Mary liked Dodos,
but her sister didn't
-----------end of sample output file
Explanation / Answer
Here is your program. It is fully tested. Please provide rating before the question ends. I am not sure if I will get the points though.
You need to create a file named ReplaceTextFromFile.java and paste the below code.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ReplaceTextFromFile extends JPanel implements ActionListener
{
static private final String newline = " ";
JButton openButton;
JTextArea log;
JFileChooser fc;
public ReplaceTextFromFile()
{
super(new BorderLayout());
// Create the log first, because the action listeners
// need to refer to it.
log = new JTextArea(5, 20);
log.setMargin(new Insets(5, 5, 5, 5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
// Create a file chooser
fc = new JFileChooser();
// Uncomment one of the following lines to try a different
// file selection mode. The first allows just directories
// to be selected (and, at least in the Java look and feel,
// shown). The second allows both files and directories
// to be selected. If you leave these lines commented out,
// then the default mode (FILES_ONLY) will be used.
//
// fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
// Create the open button. We use the image from the JLF
// Graphics Repository (but we extracted it from the jar).
openButton = new JButton("Open a File...");
openButton.addActionListener(this);
// For layout purposes, put the buttons in a separate panel
JPanel buttonPanel = new JPanel(); // use FlowLayout
buttonPanel.add(openButton);
// Add the buttons and the log to this panel.
add(buttonPanel, BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
BufferedReader input;
// Handle open button action.
if(e.getSource() == openButton)
{
int returnVal = fc.showOpenDialog(ReplaceTextFromFile.this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
// Print file name.
log.append("Opening: " + file.getName() + "." + newline);
String outputFileName = file.getAbsolutePath() + ".txt";
// Open the input and output file
try
{
// input file
input = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
// output file
File outFile = new File(outputFileName);
PrintWriter outputFilePrintWriter = new PrintWriter(outFile);
String toSearch = "aardvark";
String;
String outputLine = "";
int count = 0;
while((oneLine = input.readLine()) != null)
{
int lastIndex = 0;
outputLine = oneLine;
while(lastIndex != -1)
{
lastIndex = oneLine.toLowerCase().indexOf(toSearch, lastIndex);
if(lastIndex != -1)
{
count++;
if(count == 3)
{
count = 0;
outputLine = outputLine.substring(0, lastIndex) + "Dodo" + outputLine.substring(lastIndex + toSearch.length());
}
lastIndex += toSearch.length();
}
}
outputFilePrintWriter.println(outputLine);
}
outputFilePrintWriter.close();
// Say all done
log.append("Output file created with replaced contents. Filename: " + outputFileName);
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
else
{
log.append("Open command cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());
}
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event dispatch thread.
*/
private static void createAndShowGUI()
{
// Create and set up the window.
JFrame frame = new JFrame("FileChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add content to the window.
frame.add(new ReplaceTextFromFile());
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
// Schedule a job for the event dispatch thread:
// creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.