In this assignment, you will build an information parser. The program will read and parse text from a file, process and e

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

In this assignment, you will build an information parser. The program will read and parse text from a file, process and e

Post by answerhappygod »

In this assignment, you will build an information parser. Theprogram will read and parse text from a file, process and extractthe information needed, request new information from the user, andthen write all of the information to a new file.
There are four main files in the program: Main.java,InfoProcessor.java, MyFileReader.java, MyFileWriter.java. TheMyFileReader class will read the provided “info.txt” file and returnthe cleaned up contents of the file as an ArrayList. TheInfoProcessor class will process the array list provided byMyFileReader and extract the information needed.
The Main class will request additional information from theuser. And the MyFileWriter class will write all of the informationto a new file “personal_info.txt”. The Main class will drive theentire program, and the “main” method calls have been provided foryou. Nothing needs to be completed in the main method.
Example_output.txt
Course: MCIT_590CourseID: 590StudentID: 101Name: Tianshi
FavoriteColor: WhiteFavoriteNumber: 7
info.txt
Course: MCIT_590CourseID: 590StudentID: 101
Please do the coding below:
1.
import java.util.*;
/**
* Class to process and extract information from a list oflines.
*/
public class InfoProcessor {
/**
* List of lines to process.
*/
private ArrayList<String> lines = newArrayList<String>();
/**
* Creates InfoProcessor with given list oflines.
* @param lines to process
*/
public InfoProcessor(ArrayList<String>lines) {
this.lines = lines;
}
/**
* Gets the course name from the list oflines.
* First, finds the line that starts with"Course:".
* Second, gets the String on the very nextline, which should be the course name.
* Third, returns the String from themethod.
*
* Hint(s):
* - Use the getNextStringStartsWith(Stringstr) method to find the course name
*
* Example(s):
* - If the ArrayList<String> linescontains: "Course:" and "CIT590", and we call
* getCourseName(), we'll get "CIT590".
*
* - If the ArrayList<String> linescontains: "Course:" and "CIT 593", and we call
* getCourseName(), we'll get "CIT 593".
*
* @return course name
*/
public String getCourseName() {
// TODO Implement method
return null;
}
/**
* Gets the course ID from the list oflines.
* First, finds the line that starts with"CourseID:".
* Second, gets the String on the very nextline, which should be the course ID.
* Third, casts the String to an int andreturns it from the method.
*
* Hint(s):
* - Use the getNextStringStartsWith(Stringstr) method to find the course ID
*
* Example(s):
* - If the ArrayList<String> linescontains: "CourseID:" and "590", and we call
* getCourseId(), we'll get the int 590.
*
* - If the ArrayList<String> linescontains: "CourseID" and "593", and we call
* getCourseId(), we'll get the int 593.
*
* @return course ID
*/
public int getCourseId() {
// TODO Implement method
return 0;
}
/**
* Gets the student ID from the list oflines.
* First, finds the line that starts with"StudentID:".
* Second, gets the String on the very nextline, which should be the student ID.
* Third, casts the String to an int andreturns it from the method.
*
* Hint(s):
* - Use the getNextStringStartsWith(Stringstr) method to find the student ID
*
* Example(s):
* - If the ArrayList<String> linescontains: "StudentID:" and "101", and we call
* getStudentId(), we'll get the int 101.
*
* - If the ArrayList<String> linescontains: "StudentID" and "5554", and we call
* getStudentId(), we'll get the int5554.
*
* @return student ID
*/
public int getStudentId() {
// TODO Implement method
return 0;
}
/**
* To be used by “getCourseName”,“getCourseId”, and “getStudentId” methods.
*
* Gets the String that follows the line thatstarts with the given String.
* Gets and returns the String on the verynext line.
* If the given String doesn't exist, shouldreturn null.
*
* Example(s):
* - If an ArrayList<String> linescontains: "hello" and "world", and we call
* getNextStringStartsWith("hello"), we'llget the String "world".
*
* - If an ArrayList<String> linescontains: "Course" and "CIT590", and we call
* getNextStringStartsWith("Course"), we'llget the String "CIT590".
*
* - If an ArrayList<String> linescontains: "hello" and "world", and we call
* getNextStringStartsWith("goodbye"), we'llget null.
*
* @param str to look for in lines
* @return String from the very next line
*/
String getNextStringStartsWith(String str) {
// TODO Implement method
return null;
}
}
2.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* Class to read lines from a file.
*/
public class MyFileReader {
/**
* Name of file being read.
*/
private String filename;
/**
* Creates MyFileReader with given filenameto read.
* @param filename to read
*/
public MyFileReader(String filename) {
this.filename = filename;
}
/**
* Opens the file specified by filename andreads the text line by line.
* Cleans up each line by trimming whitespacefrom the beginning and end of each line.
* Adds each line to anArrayList<String> which is returned from the method.
* If a line is empty (does not contain anytext), it's skipped and is not added to theArrayList<String>.
*
* Example:
* - Calling getCleanContent() will open andread the file, clean up the text line by line,
* add each line containing text to anArrayList<String>, and return the ArrayList<String>from the method.
* If there is a line "nice to meetyou ", it will become "nice to meet you"
* If there is a line " hello world ", it will become "hello world"
* If there is a line "today is a niceday", it will remain "today is a nice day"
* If there is a line " ", it will beskipped and not added to the ArrayList<String>
*
* @return list of lines with no empty spacesat the beginning or end of each line
*/
public ArrayList<String> getCleanContent(){
// TODO Implement method
return null;
}
}
3.
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
/**
* Class to write list of words to a file.
*/
public class MyFileWriter {
/**
* Name of file being written to.
*/
private String filename;
/**
* Creates MyFileWriter with given filenameto write to.
* @param filename to write to
*/
public MyFileWriter(String filename) {
this.filename = filename;
}
/**
* Opens the file specified by filename andwrites each String in the given list of Strings to the file.
* Each String is written to a new line.
*
* Example:
* - CallingwriteToFile(ArrayList<String> words) will open the file, thenwrite each String in the
* given list of Strings to the file.
* Writing the course info and the additionalpersonal info to the file will be in this format.
*
* CourseName: MCIT_590
* CourseID: 590
* StudentID: 101
* Name: Tianshi
* FavoriteColor: White
* FavoriteNumber: 7
*
* (For an example of the format of theoutput file, reference "example_output.txt")
*
* @param list of words to write to thefile
*/
public void writeToFile(ArrayList<String>words) {
// TODO Implement method
}
}
Main.java
In This Assignment You Will Build An Information Parser The Program Will Read And Parse Text From A File Process And E 1
In This Assignment You Will Build An Information Parser The Program Will Read And Parse Text From A File Process And E 1 (82.94 KiB) Viewed 8 times
In This Assignment You Will Build An Information Parser The Program Will Read And Parse Text From A File Process And E 2
In This Assignment You Will Build An Information Parser The Program Will Read And Parse Text From A File Process And E 2 (80.79 KiB) Viewed 8 times
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 5 6 7 8 9 10 11 12 public class Main { 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 /** * In this assignment, you will build an information parser. * The program will read and parse text from a file, process and extract the information needed, * request new information from the user, and then write all of the information to a new file. * * The Main class will drive the entire program, and the "main" method calls have been provided for you. * Nothing needs to be completed in the main method. */ 34 35 36 37 38 39 40 41 /** * Main method to drive the program. * @param args */ public static void main(String[] args) { /* * Create new instance of file reader which reads the file "info.txt" */ MyFileReader fr = new MyFileReader("info.txt"); /* * Create new instance of file writer which writes to the file "personal_info.txt" MyFileWriter fw = new MyFileWriter ("personal_info.txt"); /* * Clean lines of text passed from the file reader */ ArrayList<String> lines = fr.getCleanContent(); /* * Create lines to write to a new file ArrayList<String> linesToWrite = new ArrayList<> (); * Create new instance of InfoProcessor
41 42 43 44 46 47 BENG NENNDO212213328654555556 57 67 69 78 81 * Create new instance of InfoProcessor */ InfoProcessor ip = new InfoProcessor (lines); Get the course name */ String courseName = ip.getCourseName(); /* * Get the course id */ int courseID = ip.getCourseId(); /* Get the student id int studentID = ip.getStudentId(); * Format the lines to be written */ String line_1 = "CourseName: "+courseName; String line_2 = "CourseID: " + Integer.toString (courseID); String line_3 = "StudentID: " + Integer.toString(studentID); * Add the Strings which will be written to the file to the ArrayList lines ToWrite */ linesToWrite.add(line_1); linesToWrite.add(line_2); linesToWrite.add(line_3); * Create scanner to request new information from user */ Scanner sc = new Scanner(System.in); // Ask the user to choose whether they want to input their personal info System.out.print("Do you want to add your personal information to the course information for CIT590?"); String option = sc.next();
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 _00 01 _02 03 04 05 06 07 _08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 String option = sc.next(); // If not, just write the lines read from info.txt and exit the program if (option.equals("n") || option.equals("N")) { } // Write the lines to new file "personal_info.txt" fw.writeToFile(linesToWrite); // Print thank you information System.out.print("Thank you, the course information has been exported to a file. "); // Exit the program System.exit(0); // If user does want to input their personal info // Ask user to input their name System.out.print("Please input your name (no spaces). "); String name = sc.next(); // Add to lines to write to file linesToWrite.add("Name: " + name); // Ask user to input their favorite color System.out.print("Please input your favorite color (no spaces). "); String color = sc.next(); // Add to lines to write to file linesToWrite.add("Favorite Color: " + color); // Ask user to input their favorite number System.out.print("Please input your favorite number (no spaces). "); String number = sc.next(); // Add to lines to write to file linesToWrite.add("FavoriteNumber: " + number); // Print status System.out.println("Thank you! We're writing your info to the file... "); // Close the scanner sc.close();
120 121 122 123 124 125 126 127 128 129 130 131 } // Close the scanner sc.close(); // Write the lines to new file "personal_info.txt" // (Reference "example_output.txt" for a sample output) fw.writeToFile(linesToWrite); // Print thank you message System.out.print("Thank you for waiting! The course information and your personal information has been exported to a file. ");
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply