In Java A comma separated value (.csv) file has been included to be used for this program. Each line contains two values, a name and a number separated by a comma (except the first line, which contains the titles for the value types). The name is that of a writer and the number refers to the number of works the writer has written. Ex. Jane Austen,6. Each line is an entry and each entry number can be updated by identifying the associated name. Once complete, the following program opens data file allWorks.csv and asks if the user wants to update entries or add new entries. All entries are stored in an array while being read in and updated. The program then writes the array to the file, overwriting the original contents. The following TODO sections must be completed. Open allWorks.csv for reading/input. Locate an entry to be updated by name (use the Java String indexOf() method) Add a new entry if the name isn't found in the file (give the entry a new number) Open file allWorks.csv for output Write contents of the array to allWorks.csv (original file is overwritten) Ex: If the input is: y J.K. Rowling 30 y Elton John y 62 n and allWorks.csv originally contains: Name,Number of Novels Jane Austen,6 Charles Dickens,20 Ernest Hemingway,9 Jack Kerouac,22 F. Scott Fitzgerald,8 Mary Shelley,7 Charlotte Bronte,5 Mark Twain,11 Agatha Christie,73 Ian Flemming,14 J.K. Rowling,14 Stephen King,54 Oscar Wilde,1 the output in allWorks.csv is: Name,Number of Novels Jane Austen,6 Charles Dickens,20 Ernest Hemingway,9 Jack Kerouac,22 F. Scott Fitzgerald,8 Mary Shelley,7 Charlotte Bronte,5 Mark Twain,11 Agatha Christie,73 Ian Flemming,14 J.K. Rowling,30 Stephen King,54 Oscar Wilde,1 Elton John,62 import java.util.Scanner; import java.util.ArrayList; import java.io.FileInputStream; import java.io.PrintWriter; import java.io.FileOutputStream; import java.io.IOException; public class DataFile { public static void main(String[] args) throws IOException { Scanner scnr = new Scanner(System.in); FileInputStream inputFileStream = null; // File input stream Scanner inFS = null; // File Scanner object FileOutputStream outputFileStream = null; PrintWriter outFS = null; String lineString = ""; // Stores line-by-line input from file char yesNo = 'n'; String updateName = ""; String updateNum = ""; String[] fileContents = new String[50]; // Array for storing and updating file contents int size = 0; int i = 0; boolean entryFound = false; System.out.println("Would you like to update allWork.csv?"); yesNo = scnr.next().charAt(0); scnr.nextLine(); while (yesNo == 'y') { size = 0; // TODO: Open file "allWorks.csv" for input System.out.println("Which entry would you like to update?"); updateName = scnr.nextLine(); while (inFS.hasNext()) { lineString = inFS.nextLine(); if(/* TODO: Locate updateName in the file */) { entryFound = true; System.out.println("Enter a new value for " + updateName); updateNum = scnr.next(); lineString = updateName + "," + updateNum; } // Storing each line in the array fileContents[size] = lineString; ++size; } if (!entryFound) { System.out.println("Entry not found. Add new entry?"); yesNo = scnr.next().charAt(0); scnr.nextLine(); if(yesNo == 'y') { System.out.println("Number of works?"); // TODO: Add new entry if the name isn't found in the file } } // TODO: Open file "allWorks.csv" for output // TODO: Write contents of the array to the allWorks.csv (original file is overwritten) System.out.println("Would you like to continue to update allWork.csv?"); yesNo = scnr.next().charAt(0); scnr.nextLine(); } outputFileStream.close(); // close() may throw IOException if fails }
In Java
A comma separated value (.csv) file has been included to be used for this
Once complete, the following program opens data file allWorks.csv and asks if the user wants to update entries or add new entries. All entries are stored in an array while being read in and updated. The program then writes the array to the file, overwriting the original contents. The following TODO sections must be completed.
- Open allWorks.csv for reading/input.
- Locate an entry to be updated by name (use the Java String indexOf() method)
- Add a new entry if the name isn't found in the file (give the entry a new number)
- Open file allWorks.csv for output
- Write contents of the array to allWorks.csv (original file is overwritten)
Ex: If the input is:
y J.K. Rowling 30 y Elton John y 62 n
and allWorks.csv originally contains:
Name,Number of Novels Jane Austen,6 Charles Dickens,20 Ernest Hemingway,9 Jack Kerouac,22 F. Scott Fitzgerald,8 Mary Shelley,7 Charlotte Bronte,5 Mark Twain,11 Agatha Christie,73 Ian Flemming,14 J.K. Rowling,14 Stephen King,54 Oscar Wilde,1
the output in allWorks.csv is:
Name,Number of Novels Jane Austen,6 Charles Dickens,20 Ernest Hemingway,9 Jack Kerouac,22 F. Scott Fitzgerald,8 Mary Shelley,7 Charlotte Bronte,5 Mark Twain,11 Agatha Christie,73 Ian Flemming,14 J.K. Rowling,30 Stephen King,54 Oscar Wilde,1 Elton John,62
import java.util.Scanner;
import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataFile {
public static void main(String[] args) throws IOException {
Scanner scnr = new Scanner(System.in);
FileInputStream inputFileStream = null; // File input stream
Scanner inFS = null; // File Scanner object
FileOutputStream outputFileStream = null;
PrintWriter outFS = null;
String lineString = ""; // Stores line-by-line input from file
char yesNo = 'n';
String updateName = "";
String updateNum = "";
String[] fileContents = new String[50]; // Array for storing and updating file contents
int size = 0;
int i = 0;
boolean entryFound = false;
System.out.println("Would you like to update allWork.csv?");
yesNo = scnr.next().charAt(0);
scnr.nextLine();
while (yesNo == 'y') {
size = 0;
// TODO: Open file "allWorks.csv" for input
System.out.println("Which entry would you like to update?");
updateName = scnr.nextLine();
while (inFS.hasNext()) {
lineString = inFS.nextLine();
if(/* TODO: Locate updateName in the file */) {
entryFound = true;
System.out.println("Enter a new value for " + updateName);
updateNum = scnr.next();
lineString = updateName + "," + updateNum;
}
// Storing each line in the array
fileContents[size] = lineString;
++size;
}
if (!entryFound) {
System.out.println("Entry not found. Add new entry?");
yesNo = scnr.next().charAt(0);
scnr.nextLine();
if(yesNo == 'y') {
System.out.println("Number of works?");
// TODO: Add new entry if the name isn't found in the file
}
}
// TODO: Open file "allWorks.csv" for output
// TODO: Write contents of the array to the allWorks.csv (original file is overwritten)
System.out.println("Would you like to continue to update allWork.csv?");
yesNo = scnr.next().charAt(0);
scnr.nextLine();
}
outputFileStream.close(); // close() may throw IOException if fails
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps