Use object oriented techniques to write the program with at
Use object oriented techniques to write the program, with at least one class and one instantiated object other than the Scanner/PrintWriter. Use private variables whenever possible. Be sure to comment your code. Also, please put some header information (comments) at the top of the program, including your name, the class name, the date and the assignment information. Please start your class name with “assn2”, then end with a name of your choosing. Use methods to perform all of the actions in all the bullet point sections below. Write a Java program to read in the provided file, named “assn2data.txt”. The data from the file should be read into an ArrayList of String type. Only open the file with the built-in Scanner class and write the file with PrintWriter. The ArrayList will be storing String objects. Write a loop to read and store each line of the “assn2data.txt” file as an item in the ArrayList. Once the reading is completed, perform the following:
1. Display on the screen the number of entries/items that are stored in the ArrayList right after reading all the items in, also display the number of characters stored.
2. Once the file is in memory, swap every other line with the previous line. This way the 1st and 2nd lines will be swapped, the 3rd and 4th lines will be swapped, etc. Loop through all the items in the ArrayList until all have been swapped. If there are an uneven number of lines at the end of the ArrayList then end the swapping portion of the project. At the end of the swapping portion, display to the user that the swapping is complete.
3. Delete every 10th line in the ArrayList.
4. Add a message to the beginning of the ArrayList, stating, “This file has been modified on: ”
5. Add a message to the end of the ArrayList, stating, “Number of lines remaining: <# of lines>” (This will be the number of lines left in the ArrayList after your modifications)
6. Make a loop to write the entire modified ArrayList to a new file called, “assn2data-modified.txt”. Only use Scanner to write the data to the file.
Idk why but for number 4 and 5 isn\'t printing out the date its been modified and number of lines remaining.
Here is the assn2data.txt: https://drive.google.com/open?id=0B6ppF_Z-qSRMS0syZEhKMXNxOWc
Here is my code:
package assignment2;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class assn2 {
public static void main(String[] args) {
//ArrayList of type String
ArrayList data = new ArrayList();
try{
Scanner s = new Scanner(new FileInputStream(\"C:/Documents/assn2data.txt\"));
String line;
int sum=0;
// Read Each line of File Using hasNextLine()
while(s.hasNextLine()){
line = s.nextLine(); // nextLine() is to retrieve
sum = sum+line.length();
data.add(line); //Adding line to ArrayList
}
System.out.println(\"The number of Entries are:\"+data.size());
System.out.println(\"The number of characters:\"+sum);
swapping(data); // Calling swapping method by passing parameter list
}catch(Exception e){
e.printStackTrace();
}
}
public static void swapping(ArrayList data) throws IOException {
System.out.println(\"Swapping line from ArrayList\");
for(int i=0;i {
Collections.swap(data, i, i+1); // Collections.sswap used to swap all data
} // using i and i+1
System.out.println(\"Swapping is Complete...\");
System.out.println(\"Deleting every 10 Line..\");
int k=9;
while(data.size()>k){
data.remove(k); // Removing object from the list for every 10 element
k=k+10;
}
data.add(0, \"*****The file has been modified on*****\");
data.add(data.size(), \"*****Number of lines remaining are******\");
System.out.println(\"Storing Data into file after being modified..\");
int j=0;
//Writing modified data into file
PrintWriter p = new PrintWriter(\"C:/Users/Documents/assn2data-modified.txt\");
BufferedWriter b = new BufferedWriter(p);
while(data.size()>j){
b.write(data.get(j));
b.newLine();
j++;
}
b.close();
p.close();
System.out.println(\"Data is stored into modified file..\");
}
}
Solution
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class assn2 {
public static void main(String[] args) {
//ArrayList of type String
ArrayList data = new ArrayList();
try{
Scanner s = new Scanner(new FileInputStream(\"C:/Documents/assn2data.txt\"));
String line;
int sum=0;
// Read Each line of File Using hasNextLine()
while(s.hasNextLine()){
line = s.nextLine(); // nextLine() is to retrieve
sum = sum+line.length();
data.add(line); //Adding line to ArrayList
}
System.out.println(\"The number of Entries are:\"+data.size());
System.out.println(\"The number of characters:\"+sum);
swapping(data); // Calling swapping method by passing parameter list
}catch(Exception e){
e.printStackTrace();
}
}
public static void swapping(ArrayList data) throws IOException {
System.out.println(\"Swapping line from ArrayList\");
for(int i=0;i {
Collections.swap(data, i, i+1); // Collections.sswap used to swap all data
} // using i and i+1
System.out.println(\"Swapping is Complete...\");
System.out.println(\"Deleting every 10 Line..\");
int k=9;
while(data.size()>k){
data.remove(k); // Removing object from the list for every 10 element
k=k+10;
}
data.add(0, \"*****The file has been modified on*****\");
data.add(data.size(), \"*****Number of lines remaining are******\");
System.out.println(\"Storing Data into file after being modified..\");
int j=0;
//Writing modified data into file
PrintWriter p = new PrintWriter(\"C:/Users/Documents/assn2data-modified.txt\");
BufferedWriter b = new BufferedWriter(p);
while(data.size()>j){
b.write(data.get(j));
b.newLine();
j++;
}
b.close();
p.close();
System.out.println(\"Data is stored into modified file..\");
}
}


