How do I add another txtfile to this Im trying to add this t

How do I add another txt.file to this? I\'m trying to add this to below...can someone help me please?

US;NONRESIDENT;123456;Jones;123 Cooper St;Arlington;Texas;76019;12345

INT;ACTIVE;A-654789;Degrassey;18 Love Lane;Dallas;Texas;75052;67123

INT;INACTIVE;A-543891;Franco;1201 Trail Road;Euless;Texas;74032;19814

US;RESIDENT;345123;Hughes;1803 Division;Fort Worth;Texas;76034;674532

US;RESIDENT;988776;Cooper;111 Marsh Lane;Bedford;Texas;76111;90906

INT;INACTIVE;B-577463;Patel;2218 Border St;Arlington;Texas;76015;81832


package writefiles;

import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class WriteFiles {
  
private static Formatter output;
private static Scanner input;
  
public static void main(String[] args) {
  
ArrayList<Course> coursesList = new ArrayList<Course>();
writeTextFile();
readTextFile(coursesList);
  
  
for (Course c: coursesList){
System.out.println(c.toString());
}
  
writeSerFile(coursesList);
readSerFile();
  
}
  
public static void writeTextFile(){
  
try
{
Formatter output = new Formatter(\"courses.txt\");
output.format(\"%s;%s;%s;%d;%d%n\",\"Python\", \"INSY 3300\", \"560-650 MW\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Networking\", \"INSY 3303\", \"530-650 TR\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"DBMS\", \"INSY 3304\", \"900-950 MWF\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Analysis&Design\", \"INSY 3305\", \"700-820 TR\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Java I\", \"INSY 4305\", \"700-820 TR\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Java II\", \"INSY 4306\", \"530-650 TR\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Mobile App\", \"INSY 4308\", \"200-320 TR\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Web Development\", \"INSY 4315\", \"1000-1050 MWF\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Resource Management\", \"INSY 4325\", \"100-220 TR\", 1, 3);
output.flush();
output.close();
}
  
catch(IOException ioe){
ioe.printStackTrace();
}
}
  
public static void readTextFile(ArrayList<Course> coursesList){
  
String line;
String values[];
  
try{
input = new Scanner(getFile());
  
while(input.hasNext())
{
line = input.nextLine();
values = line.split(\";\");
  
coursesList.add(new Course(values[0], values[1], values[2],
Integer.parseInt(values[3]), Integer
.parseInt(values[4])));
  
}
}
  
catch (IOException ioe){
ioe.printStackTrace();
}
  
}
  
public static void writeSerFile(ArrayList<Course>coursesList){
ObjectOutputStream output;
  
try{
output = new ObjectOutputStream(new FileOutputStream(\"courses.ser\"));
  
for (Course c: coursesList){
output.writeObject(c);
}
}
  
catch(IOException ioe){
ioe.printStackTrace();
}
}
  
public static void readSerFile(){
ObjectInputStream input;
Course obj;
  
try{
input = new ObjectInputStream(new FileInputStream(getFile()));
  
while(true){
obj = (Course)input.readObject();
System.out.println(obj.toString());
}
}
catch(EOFException eof){
  
}
  
catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}
  
catch(IOException ioe){
ioe.printStackTrace();
}
}
  
public static File getFile()
{
JFileChooser fileChooser = new JFileChooser();
  
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.CANCEL_OPTION){
System.exit(1);
}
  
File fileName = fileChooser.getSelectedFile();
  
if ((fileName == null) || (fileName.getName().equals(\"\"))){
JOptionPane.showMessageDialog(null, \"Invalid Option\",
\"Invalid Option\", JOptionPane.ERROR_MESSAGE);
  
System.exit(1);
}
return fileName;
}
  
}

Solution

We need to create new course constructor and new function to read file. And remaining function remins same.

//course constructor should be
public course(String val1, String val2, String val3,String val4, String val5,String val6,String val7,int val8, int val9){
   // initialise these variables in course file
}

-------------------------------------------------------------------------------------------------
// now I am modifying in our exact file

import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class WriteFiles {
  
private static Formatter output;
private static Scanner input;
  
public static void main(String[] args) {
  
ArrayList<Course> coursesList = new ArrayList<Course>();
writeTextFile();
readTextFile(coursesList);
readTextFileNew(coursesList); // calling seperate new method to read new format file
  
for (Course c: coursesList){
System.out.println(c.toString());
}
  
writeSerFile(coursesList);
readSerFile();
  
}
  
public static void writeTextFile(){
  
try
{
Formatter output = new Formatter(\"courses.txt\");
output.format(\"%s;%s;%s;%d;%d%n\",\"Python\", \"INSY 3300\", \"560-650 MW\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Networking\", \"INSY 3303\", \"530-650 TR\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"DBMS\", \"INSY 3304\", \"900-950 MWF\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Analysis&Design\", \"INSY 3305\", \"700-820 TR\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Java I\", \"INSY 4305\", \"700-820 TR\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Java II\", \"INSY 4306\", \"530-650 TR\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Mobile App\", \"INSY 4308\", \"200-320 TR\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Web Development\", \"INSY 4315\", \"1000-1050 MWF\", 1, 3);
output.format(\"%s;%s;%s;%d;%d%n\",\"Resource Management\", \"INSY 4325\", \"100-220 TR\", 1, 3);
output.flush();
output.close();
}
  
catch(IOException ioe){
ioe.printStackTrace();
}
}
  
public static void readTextFile(ArrayList<Course> coursesList){
  
String line;
String values[];
  
try{
input = new Scanner(getFile());
  
while(input.hasNext())
{
line = input.nextLine();
values = line.split(\";\");
  
coursesList.add(new Course(values[0], values[1], values[2],
Integer.parseInt(values[3]), Integer
.parseInt(values[4])));
  
}
}
  
catch (IOException ioe){
ioe.printStackTrace();
}
  
}
  
   public static void readTextFileNew(ArrayList<Course> coursesList){
  
String line;
String values[];
  
try{
input = new Scanner(getFile());
  
while(input.hasNext())
{
line = input.nextLine();
values = line.split(\";\");
  
coursesList.add(new Course(values[0], values[1], values[2],values[3],values[4],values[5],values[6]
Integer.parseInt(values[7]), Integer.parseInt(values[8])));
  
}
}
  
catch (IOException ioe){
ioe.printStackTrace();
}
  
}
  

public static void writeSerFile(ArrayList<Course>coursesList){
ObjectOutputStream output;
  
try{
output = new ObjectOutputStream(new FileOutputStream(\"courses.ser\"));
  
for (Course c: coursesList){
output.writeObject(c);
}
}
  
catch(IOException ioe){
ioe.printStackTrace();
}
}
  
public static void readSerFile(){
ObjectInputStream input;
Course obj;
  
try{
input = new ObjectInputStream(new FileInputStream(getFile()));
  
while(true){
obj = (Course)input.readObject();
System.out.println(obj.toString());
}
}
catch(EOFException eof){
  
}
  
catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}
  
catch(IOException ioe){
ioe.printStackTrace();
}
}
  
public static File getFile()
{
JFileChooser fileChooser = new JFileChooser();
  
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.CANCEL_OPTION){
System.exit(1);
}
  
File fileName = fileChooser.getSelectedFile();
  
if ((fileName == null) || (fileName.getName().equals(\"\"))){
JOptionPane.showMessageDialog(null, \"Invalid Option\",
\"Invalid Option\", JOptionPane.ERROR_MESSAGE);
  
System.exit(1);
}
return fileName;
}
  
}

How do I add another txt.file to this? I\'m trying to add this to below...can someone help me please? US;NONRESIDENT;123456;Jones;123 Cooper St;Arlington;Texas;
How do I add another txt.file to this? I\'m trying to add this to below...can someone help me please? US;NONRESIDENT;123456;Jones;123 Cooper St;Arlington;Texas;
How do I add another txt.file to this? I\'m trying to add this to below...can someone help me please? US;NONRESIDENT;123456;Jones;123 Cooper St;Arlington;Texas;
How do I add another txt.file to this? I\'m trying to add this to below...can someone help me please? US;NONRESIDENT;123456;Jones;123 Cooper St;Arlington;Texas;
How do I add another txt.file to this? I\'m trying to add this to below...can someone help me please? US;NONRESIDENT;123456;Jones;123 Cooper St;Arlington;Texas;
How do I add another txt.file to this? I\'m trying to add this to below...can someone help me please? US;NONRESIDENT;123456;Jones;123 Cooper St;Arlington;Texas;

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site