Question

In: Computer Science

Create a Java class named ReadWriteCSV that reads the attached cis425_io.txt file that you will save...

Create a Java class named ReadWriteCSV that reads the attached cis425_io.txt file that you will save in your workspace as src/cis425_io.txt and displays the following table:
--------------------------
| No | Month Name | Days |
--------------------------
| 1 | January | 31 |
| 2 | February | 28 |
| 3 | March | 31 |
| 4 | April | 30 |
| 5 | May | 31 |
| 6 | June | 30 |.
| 7 | July | 31 |
| 8 | August | 31 |
| 9 | September | 30 |
| 10 | October | 31 |
| 11 | November | 30 |
| 12 | December | 31 |
--------------------------
Write that file out in reverse order to a file named: src/cis425_ior.txt

public class ReadWriteCSV {
// Put class properties here
// Add method, processInfile(), to open file and read here
   void processInfile()
   {
       File filename = new File ("cis425_io.txt");
       Scanner in = new Scanner(System.in); // System.in is an InputStream
       Scanner inFile = new Scanner("cis425_io.txt");
   }
// add method, displayTable(), here to display the table
   void displayTable() {
      
   }
// add method, writeOutfile(), here to open and write the data in reverse order to the new file
   void writeOutfile() {
      
   }
// add main() method here
   public static void main(String[] args) {
      
   }
}

Solutions

Expert Solution

// For any doubt, feel free to comment.

CODE with Comments:-

package readwritecsv;
import java.io.*;
import java.util.*;

public class ReadWriteCSV {

//Arraylist which will store the src/cis425_io.txt details line by line
static ArrayList<String> arr = new ArrayList<String>();
static void processInfile() throws FileNotFoundException
{
File filename = new File ("src/cis425_io.txt");
Scanner in = new Scanner(System.in); // System.in is an InputStream
Scanner inFile = new Scanner(filename);

//Reading till end of the file
while(inFile.hasNextLine()){
String line = inFile.nextLine();
//Adding line to arraylist
arr.add(line);
}

}

static void displayTable(){
System.out.println("--------------------------");
System.out.println("| No | Month Name | Days |");
System.out.println("--------------------------");
  
//Traversing through arraylist to print table
for(int i=0;i<arr.size();i++){
String line = arr.get(i);
//Split each line by space so that we get index,monthname,days seperately
String[] words = line.split(" ");
System.out.println("| " + words[0] + " | " + words[1] + " | " + words[2] + " |");
}
System.out.println("--------------------------");
}
  
static void writeOutfile() throws IOException{
//Opening file
FileWriter writer = new FileWriter("src/cis425_ior.txt");
BufferedWriter bw = new BufferedWriter(writer);
  
//Writing into file src/cis425_ior.txt by traversing the arraylist reverse
for(int i=arr.size()-1;i>=0;i--){
bw.write(arr.get(i) + "\n");
}
  
bw.close();
  
}
  
public static void main(String[] args) throws FileNotFoundException, IOException {
//Calling all function in a sync order
processInfile();
displayTable();
writeOutfile();
}   
}

Code Snippets:-

Output:-


Related Solutions

Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
In java: -Create a class named Animal
In java: -Create a class named Animal
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Create a class named Employee and its child class named Salesperson. Save each class in its...
Create a class named Employee and its child class named Salesperson. Save each class in its own file. Name your class and source code file containing the main method homework.java. Make sure each class follows these specifications: 1. An employee has a name (String), employee id number (integer), hourly pay rate (double), a timesheet which holds the hours worked for the current week (double array) and email address (String). A salesperson also has a commission rate, which is a percentage...
1. Create a1. Create a new java file named Name.java that should have Name class based...
1. Create a1. Create a new java file named Name.java that should have Name class based on new java file named Name.java that should have Name class based on the following UML Name - first: String - last: String + Name () + Name (String firstName, String lastName) + getName () : String + setName (String firstName, String lastName) : void + getFirst () : String + setFirst (String firstName) : void + getLast () : String + setLast (String...
Create a file named Good1 nano Good1 Type Welcome in the file and save it. What...
Create a file named Good1 nano Good1 Type Welcome in the file and save it. What command gives you a long listing of your filenames in the current directory, including permissions attached to each file. What command would you use to change a file’s permissions to include read, write and execute permission for the owner of the file only. Explain the following file permissions    a) 777          b) 765          c) 400          d) 666          e) 600         ...
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
Create a Java class file for an Account class. In the File menu select New File......
Create a Java class file for an Account class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Account. For Package: select csci1011.lab8. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab8; /** * * @author Your Name */ public class Account { } Implement the Account...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
Write a program in Java that reads an input text file (named: input.txt) that has several...
Write a program in Java that reads an input text file (named: input.txt) that has several lines of text and put those line in a data structure, make all lowercase letters to uppercase and all uppercase letters to lowercase and writes the new lines to a file (named: output.txt).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT