Java Serialization issues - java
I have this code that writes an object:
FileOutputStream fileOut = new FileOutputStream("Model.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);
out.close();
fileOut.close();
And this code that loads the object:
Model m = null;
try {
FileInputStream fileIn = new FileInputStream("Model.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
m = (Model) in.readObject();
in.close();
fileIn.close();
} catch (Exception e) {}
setStudentList(m.getStudentList());
setModuleList(m.getModuleList());
I'm pretty sure that saving works, as when I opened the file in notepad++ I saw most of the data that I had saved, but when I load there is no data in module list.
Full source code:
import java.io.*;
import java.util.*;
public class Model implements java.io.Serializable {
private Student[] studentList = new Student[0];
private Module[] moduleList = new Module[0];
public void menu() {
while (true) {
System.out.println ("MENU");
System.out.println ("");
System.out.println (" 1 - Run Tests");
System.out.println (" 2 - Add Student");
System.out.println (" 3 - Add Module");
System.out.println (" 4 - Add Student To Module");
System.out.println (" 5 - Save System (Text file)");
System.out.println (" 6 - Load System (Text file)");
System.out.println (" 7 - Save System (Serialized)");
System.out.println (" 8 - Load System (Serialized)");
System.out.println (" 9 - Print Report");
System.out.println ("");
System.out.print ("Enter choice: ");
String input = keyboard.readString();
switch (input) {
case "1" :
runTests();
break;
case "2" :
System.out.print("First Name : ");
String fN = keyboard.readString();
System.out.print("Surname : ");
String sN = keyboard.readString();
System.out.print("Course Code : ");
String c = keyboard.readString();
System.out.print("User ID : ");
String iD = keyboard.readString();
AddStudent(iD, sN, fN, c);
break;
case "3" :
System.out.print("Module Code : ");
String code = keyboard.readString();
String[] temp = new String[0];
AddModule(code,temp);
break;
case "4" :
System.out.print("Module Code : ");
code = keyboard.readString();
Module m = findAModule(code);
if (m != null) {
System.out.print("User ID : ");
iD = keyboard.readString();
Student s = findAStudent(iD);
if (s != null) {
m.addThisStudent(s);
} else {
System.out.println("Student not found");
}
} else {
System.out.println("Module not found");
}
break;
case "5" :
saveToTextFiles();
break;
case "6" :
loadFromTextFiles();
break;
case "7" :
saveSerialized();
break;
case "8" :
break;
case "9" :
printReport();
break;
}
}
}
public void runTests() {
loadFromTextFiles();
saveSerialized();
printReport();
}
public void loadFromTextFiles() {
studentList = new Student[0];
moduleList = new Module[0];
try {
Scanner fileReader = new Scanner(new InputStreamReader(new FileInputStream("students.txt")));
int num = fileReader.nextInt(); fileReader.nextLine();
for (int i = 0; i < num; i++) {
String u = fileReader.nextLine();
String sn = fileReader.nextLine();
String fn = fileReader.nextLine();
String c = fileReader.nextLine();
AddStudent(u, sn, fn, c);
}
fileReader.close();
fileReader = new Scanner(new InputStreamReader(new FileInputStream("modules.txt")));
num = fileReader.nextInt(); fileReader.nextLine();
for (int i = 0; i < num; i++) {
String code = fileReader.nextLine();
int numOfStudents = fileReader.nextInt(); fileReader.nextLine();
String[] students = new String[numOfStudents];
for (int j = 0; j < numOfStudents; j++) {
students[j] = fileReader.nextLine();
}
AddModule(code, students);
}
fileReader.close();
} catch (IOException e) {}
}
public void saveToTextFiles () {
try {
PrintWriter outfile = new PrintWriter(new OutputStreamWriter (new FileOutputStream("students.txt")));
outfile.println(studentList.length);
for (int i = 0; i < studentList.length; i++) {
outfile.println(studentList[i].getUID());
outfile.println(studentList[i].getSN());
outfile.println(studentList[i].getFN());
outfile.println(studentList[i].getDegree());
}
outfile.close();
outfile = new PrintWriter(new OutputStreamWriter (new FileOutputStream("modules.txt")));
outfile.println(moduleList.length);
for (int i = 0; i < moduleList.length; i++) {
outfile.println(moduleList[i].getCode());
outfile.println(moduleList[i].getStudents().length);
for (int j = 0; j < moduleList[i].getStudents().length; j++) {
outfile.println(moduleList[i].getStudents()[j]);
}
}
outfile.close();
} catch (IOException e) {}
}
public void saveSerialized() {
try {
FileOutputStream fileOut = new FileOutputStream("Model.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);
out.close();
fileOut.close();
FileOutputStream fileOut2 = new FileOutputStream("Module.ser");
ObjectOutputStream out2 = new ObjectOutputStream(fileOut2);
out2.writeObject(studentList);
out2.close();
fileOut2.close();
FileOutputStream fileOut3 = new FileOutputStream("Student.ser");
ObjectOutputStream out3 = new ObjectOutputStream(fileOut3);
out3.writeObject(moduleList);
out3.close();
fileOut3.close();
} catch (IOException e) {}
}
public void loadSerialized() {
Model m = null;
try {
FileInputStream fileIn = new FileInputStream("Model.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
m = (Model) in.readObject();
in.close();
fileIn.close();
} catch (Exception e) {}
setStudentList(m.getStudentList());
setModuleList(m.getModuleList());
}
private Module[] getModuleList() {
return moduleList;
}
private Student[] getStudentList() {
return studentList;
}
private void setModuleList(Module[] m) {
moduleList = m.clone();
}
private void setStudentList(Student[] s) {
studentList = s.clone();
}
private void AddModule(String code, String[] students) {
int length = moduleList.length;
Module NewArray[] = new Module[length + 1];
for (int i = 0; i < length + 1; i++) {
if (i < length) {
NewArray[i] = new Module(moduleList[i]);
}
}
NewArray[length] = new Module(code, students);
moduleList = NewArray.clone();
}
private void AddStudent(String u, String sn, String fn, String c) {
int length = studentList.length;
Student NewArray[] = new Student[length + 1];
for(int i = 0; i < length + 1; i++) {
if (i < length) {
NewArray[i] = new Student(studentList[i]);
}
}
NewArray[length] = new Student(u, sn, fn, c);
studentList = NewArray.clone();
}
public void printReport() {
for (int i = 0; i < moduleList.length; i++) {
System.out.println(moduleList[i].toString(this));
}
}
public Student findAStudent(String uid) {
for (int i = 0; i < studentList.length; i++) {
if (studentList[i].getUID().compareTo(uid) == 0) {
return studentList[i];
}
}
return null;
}
public Module findAModule(String code) {
for (int i = 0; i < moduleList.length; i++) {
if (moduleList[i].getCode().compareTo(code) == 0) {
return moduleList[i];
}
}
return null;
}
}
Look at your code sample, in particular the switch statement:
switch (input) {
...
case "8" :
break;
...
}
I'd assume the method loadSerialized should be called there but it's missing and is not called anywhere else in the code.
Once you actually call the method that does the loading, the code will work, assuming you have declared a serialVersionUID for both Student and Module.
Edit: why using serialization to persist objects is a bad idea
In simple terms, using serialization to persist objects is brittle.
An object's serialized form is tied to its class. Classes tend to change over time, meaning the serialized instance of the old cannot be loaded into the new.
While you can work round this by setting a serialVersionUID doing so introduces a reverse of the problem where, in the case new fields are introduced, the new cannot be read into objects of the old class, which can be a problem if you do rolling updates to the deployed system.
There's a host of other reasons - it's not easily readable (meaning you can't update it like you would a database or XML/JSON doc), it's inefficient, etc.
Related
How can I take the length from the text files in java?
I am new in java. Firstly, I'm sorry for my English. I wrote a code to merge two different txt files in the mergedFile.txt with determined data from the data1.txt and data2.txt. I create 5 different arrays to use them better but I cannot learn the length of words in the textfiles so arrays are using determined parameter. If I want to add another student, this codes don't work. Can you help me? data1.txt ID,Name,LastName,Department 12345,John,Samon,Computer Science 14524,David,Souza,Electric and Electronic . . data2.txt ID,Q1,Q2,Q3,Midterm,Final 12345,100,90,75,89,100 14524,80,70,65,15,90 . margedFile.txt ID,Name,Q_Average,Midterm,Final,Department 12345,John,88.3,89,100,Computer Science 14524,David,67.0,100,70,Electric and Electronic This is ReadData Class import java.io.FileInputStream;//import java.io library import java.util.Scanner;//import scanner library public class ReadData { public static String[] Read(String filename,String filename2) { Scanner scan = null; Scanner scan1 = null;/ FileInputStream input1 = null; FileInputStream input = null; String[] result = new String[3]; try { input = new FileInputStream(filename); scan = new Scanner(input); input1 = new FileInputStream(filename2); scan1 = new Scanner(input1); String[][] myArray = new String[4][4]; String[][] myArray1 = new String[4][6]; while(scan.hasNext() || scan1.hasNext()) { for (int i = 0; i < myArray.length; i++) { String[] split = scan.nextLine().trim().split(","); for (int j = 0; j < split.length; j++) { myArray[i][j] = split[j]; } } for (int i = 0; i < myArray1.length; i++) { String[] split1 = scan1.nextLine().trim().split(","); for (int j = 0; j < split1.length; j++) { myArray1[i][j] = split1[j]; } } } int[][] quiz = new int[3][3]; double[] average = new double[3]; int sum = 0; double averagee = 0; for (int i = 0; i < quiz.length; i++) { for (int j = 0; j < quiz.length; j++) { quiz[i][j] = Integer.parseInt(myArray1[i+1][j+1]); sum += quiz[i][j]; } averagee = sum/quiz.length; average[i] = averagee; sum = 0; } for (int i = 1; i < myArray1.length; i++) { for (int j = 1; j < myArray1.length; j++) { if(myArray[i][0].equalsIgnoreCase(myArray1[j][0])) { result[i-1] = "\n" + myArray[i][0] + " "+ myArray[i][1] + " " + (average[j-1]) +" "+ myArray1[j][4] +" " + myArray1[j][5] + " "+ myArray[i][3]; //System.out.println(Arrays.deepToString(myArray[i]) + " " + Arrays.deepToString(myArray1[j])); } } } //System.out.println(Arrays.deepToString(quiz)); //System.out.println(Arrays.toString(average)); } catch(Exception e) { System.out.println(e); } return result; } } This is WriteData class import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.Arrays; public class WriteData extends ReadData { void Write(String filename) { PrintWriter writer = null; FileOutputStream output = null; try { output = new FileOutputStream(filename); writer = new PrintWriter(output); writer.print("ID,Name,Q_Average,Midterm,Final,Department "); writer.print(Arrays.toString(Read("data1.txt", "data2.txt"))); writer.flush(); writer.close(); } catch (Exception e) { // TODO: handle exception } } }
Importing from text file into Java Array
Currently I am trying to import data from a text file regarding pets and doctors, and sending it to my "petArray" and "doctorArray". I am extremely new to Java and as such am having great difficulty. This is what ive attempted to do, but it doesnt seem too work. Please see attached Java code and text file (screenshot at link). public void readFile() { String fileName = "VetManagement.txt"; Scanner inputStream = null; try { inputStream = new Scanner(new File(fileName)); } catch (FileNotFoundException e) { System.out.println("Error opening file: " + fileName); System.exit(0); } while (inputStream.hasNextLine()) { String line = inputStream.nextLine(); String initName = ""; String initSize = ""; String initType = ""; String initDoctor = ""; double initWeight = 0.0; int initAge = 0; if (line.equals("Pets")) { inputStream.nextLine(); if (line.equals("type cat")) { initType = "cat"; System.out.print(initType); } else if (line.equals("type dog")) { initType = "dog"; System.out.print(initType); } inputStream.nextLine(); if (line.equals("size small")) { initSize = "small"; } else if (line.equals("size medium")) { initSize = "medium"; } else if (line.equals("size large")) { initSize = "large"; } else System.out.println("error"); inputStream.nextLine(); if (line.startsWith("name")) { initName = inputStream.next(); } else { System.out.println("error"); } inputStream.nextLine(); if (line.startsWith("weight")) { initWeight = inputStream.nextDouble(); } else { System.out.println("error"); } inputStream.nextLine(); if (line.startsWith("age")) { initAge = inputStream.nextInt(); } else { System.out.println("error"); } inputStream.nextLine(); if (line.startsWith("doctor")) { initDoctor = inputStream.toString(); } else { System.out.println("error"); } petArray[sumPets] = new Pet(); petArray[sumPets].setType(initType); petArray[sumPets].setSize(initSize); petArray[sumPets].setName(initName); petArray[sumPets].setWeight(initWeight); petArray[sumPets].setAge(initAge); petArray[sumPets].setDoctorName(initDoctor); } else if (line.equals("Doctors")) ; } inputStream.close(); } TEXT FILE: Pets type cat size small name Lara weight 4 age 5 doctor Joao type dog size large name Biro weight 15 age 12 doctor Maria type cat size large name Benny weight 7 age 10 doctor no doctor assigned Doctors name Joao specialisation cat name Maria specialisation dog
nextLine can solve the problem easily no need to over-complicate the work As the TEXT FILE contains information in format like type cat size small name Lara weight 4 age 5 doctor Joao You can easily store information in desired variable using nextLine = inputStream.nextLine().split(" "); where nextLine[0] represent first column and nextLine[1] represent second column I Hope this helps let me know if you have any other problem here is FULL CODE (in case you need) public static void readFile() { String fileName = "F:\\document\\eclipse\\JavaAZ\\src\\VetManagement.txt"; Scanner inputStream = null; try { inputStream = new Scanner(new File(fileName)); } catch (FileNotFoundException e) { System.out.println("Error opening file: " + fileName); System.exit(0); } String[] name = new String[100]; String[] size = new String[100]; String[] type = new String[100]; String[] doctor = new String[100]; double[] weight = new double[100]; int[] age = new int[100]; if (inputStream.hasNextLine()) { String[] nextLine = inputStream.nextLine().split(" "); int petCounter = 0; int doctorCounter = 0; String workingArray = new String(nextLine[0]); while(inputStream.hasNextLine()) { if(workingArray.equals("Pets")) { nextLine = inputStream.nextLine().split(" "); if (nextLine[0].equals("Doctors")) { workingArray = "Doctors"; continue; } if (nextLine[0].equals("type")) { type[petCounter] = nextLine[1]; //System.out.println(type); } else System.out.println("type error"); nextLine = inputStream.nextLine().split(" "); if (nextLine[0].equals("size")) { size[petCounter] = nextLine[1]; //System.out.println(size); } else System.out.println("size error"); nextLine = inputStream.nextLine().split(" "); if (nextLine[0].equals("name")) { name[petCounter] = nextLine[1]; //System.out.println(name); } else System.out.println("name error"); nextLine = inputStream.nextLine().split(" "); if (nextLine[0].equals("weight")) { weight[petCounter] = Double.parseDouble(nextLine[1]); //System.out.println(weight); } else System.out.println("weight error"); nextLine = inputStream.nextLine().split(" "); if (nextLine[0].equals("age")) { age[petCounter] = Integer.parseInt(nextLine[1]); //System.out.println(age); } else System.out.println("age error"); nextLine = inputStream.nextLine().split(" "); if (nextLine[0].equals("doctor")) { doctor[petCounter] = nextLine[1]; //System.out.println(doctor); } else System.out.println("doctor error"); petCounter++; } else if(workingArray.equals("Doctors")) { // CODE HERE doctorCounter++; break; } } } System.out.println("PET NAME: "+name[0]+" and its Weight: "+weight[0]); inputStream.close(); }
if (line.equals("Pets")) { String nextLine = inputStream.nextLine(); if (nextLine.equals("type cat")) { initType = "cat"; System.out.print(initType); } else if (nextLine.equals("type dog")) { initType = "dog"; System.out.print(initType); } String lineAfterThat=inputStream.nextLine(); You have to store every line before you can do anything with that. You are assuming that inputStream.nextLine() reads the same line again and again, but each time you use inputStream.nextLine() it reads the next line of the file and reaches the end of the file eventually. That's what is wrong. You're not understanding the way Scanner works Use this: if (line.equals("Pets")) { String nextLine = inputStream.nextLine(); if (nextLine.equals("type cat")) { initType = "cat"; System.out.print(initType); } else if (nextLine.equals("type dog")) { initType = "dog"; System.out.print(initType); } String lineAfterThat=inputStream.nextLine(); if (lineAfterThat.equals("size small")) { initSize = "small"; } else if (lineAfterThat.equals("size medium")) { initSize = "medium"; } else if (lineAfterThat.equals("size large")) { initSize = "large"; } else System.out.println("error"); String nextFirstWord=inputStream.next(); //so that it reads only till the space if (nextFirstWord.equals("name")) { initName = inputStream.nextLine(); } else { System.out.println("error"); } String ageLineFirstWord = inputStream.next(); if (ageLineFirstWord .equals("age")) { initAge =inputStream.nextInt(); } else { System.out.println("error"); } inputStream.nextLine(); //this is to move the scanner to the nextline String doctorLineFirstWord = inputStream.next(); if (doctorLineFirstWord .equals("doctor")) { initDoctor = inputStream.nextLine(); } else { System.out.println("error"); }
How to display Object array in JTable?
This is my code which I am using but when I am trying to print dataArray object, then data is not show in JTable. Which model properties of table to print Object array values can used and how? public class ShowAddressForm extends javax.swing.JFrame { Object data[][]; Object dataArray[][]; int count = 0; String st; public ShowAddressForm(String fname , String str) { super(fname); st = str; initComponents(); fillTable(); } public void fillTable() { int count = 0; String str; try { BufferedReader br = new BufferedReader(new FileReader("D:\\JavaPrograms\\Contact Management System\\InputFiles\\AddressFile")); while((str = br.readLine()) != null) { count++; } br.close(); } catch (Exception e) { } Object id; Object name; data = new Object[count][7]; int i = 0 , j = 0 , m; try { BufferedReader buffrea = new BufferedReader(new FileReader("D:\\JavaPrograms\\Contact Management System\\InputFiles\\AddressFile")); while((str = buffrea.readLine()) != null) { StringTokenizer token = new StringTokenizer(str , "*"); int n = token.countTokens(); id = token.nextElement(); name = token.nextElement(); String strNameLow = name.toString().toLowerCase(); String strNameUpp = name.toString().toUpperCase(); if(strNameLow.startsWith(st.toLowerCase()) || strNameUpp.startsWith(st.toUpperCase())) { data[i][0] = id; data[i][1] = name; for(j = 2 ; j < n ; j++) { data[i][j] = token.nextElement(); } i = i + 1; } } buffrea.close(); } catch(IOException ioe){ System.out.println("Error : " + ioe.toString()); } dataArray = new Object[i][7]; for(int a = 0 ; a < i ; a++) { for(int b = 0 ; b < 7 ; b++) { dataArray[a][b] = data[a][b]; } } //Here is the code to print dataArray object which i used but it is not working, when i am run my program it is print "[Ljava.lang.Object;#1cc2e30" in table's first cell[0][0] position DefaultTableModel model = (DefaultTableModel)this.data_table.getModel(); model.addRow(dataArray); }
I filled data in a JTable like this. You might want to give it a try adapting it to your code. Variable and stuff are in spanish, just replace them with what you need. In my case it's a table with 4 columns representing a date, a score, duration and max viewers. private void fillJTable(){ //creating data to add into the JTable. Here you might want to import your proper data from elsewhere Date date = new Date(); UserReplay rep1 = new UserReplay(date, 12, 13,14); UserReplay rep2 = new UserReplay(date, 2,34,5); ArrayList<UserReplay> usuaris = new ArrayList<>(); usuaris.add(rep1); usuaris.add(rep2); //----Filling Jtable------ DefaultTableModel model = (DefaultTableModel) view.getTable().getModel(); model.addColumn("Fecha"); model.addColumn("Puntuación"); model.addColumn("Tiempo de duración"); model.addColumn("Pico máximo de espectadores"); for (int i = 0; i < usuaris.size(); i++){ Vector<Date> fecha = new Vector<>(Arrays.asList(usuaris.get(i).getDate())); Vector<Integer> puntuacion = new Vector<>(Arrays.asList(usuaris.get(i).getPuntuacion())); Vector<Integer> tiempo = new Vector<>(Arrays.asList(usuaris.get(i).getTiempo())); Vector<Integer> espectadors = new Vector<>(Arrays.asList(usuaris.get(i).getTiempo())); Vector<Object> row = new Vector<Object>(); row.addElement(fecha.get(0)); row.addElement(puntuacion.get(0)); row.addElement(tiempo.get(0)); row.addElement(espectadors.get(0)); model.addRow(row); } }
How to input Strings into a 2D array
I have this code and I need to export the strings 'Name','Testav','HWav','Lows','grade' into a 2D array with the columns being the following respectively. I then need to export the 2D array into a csv file. Any help would be greatly appreciated. import java.util.*; import java.io.*; import java.io.PrintWriter; import java.text.*; public class ComputeGrades { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter the name of the input file: "); String input_name = in.next(); System.out.printf("Please enter the name of the output CSV file: "); String csv_name = in.next(); System.out.printf("Please enter the name of the output pretty-print file: "); String pretty_name = in.next(); processGrades(input_name, csv_name, pretty_name); System.out.printf("\nExiting...\n"); } public static void processGrades (String input_name, String csv_name, String pretty_name) { PrintWriter csv = null; PrintWriter pretty = null; String[][] data = readSpreadsheet(input_name); boolean resultb = sanityCheck(data); int length = data.length; ArrayList<String> test_avg = new ArrayList<String>(); ArrayList<String> HW_avg = new ArrayList<String>(); ArrayList<String> NAME = new ArrayList<String>(); ArrayList<String> ColN = new ArrayList<String>(); ArrayList<String> Hell = new ArrayList<String>(); String[][] kill_me = new String[length][]; for(int row = 1; row<length; row++) { String name = data[row][0]; String name2 = data[row][1]; String Name = name+" "+name2; int test1 = Integer.parseInt(data[row][2]); int test2 = Integer.parseInt(data[row][3]); int test3 = Integer.parseInt(data[row][4]); int Test = (test1+test2+test3)/3; String Testav = Integer.toString(Test); int hw1 = Integer.parseInt(data[row][5]); int hw2 = Integer.parseInt(data[row][6]); int hw3 = Integer.parseInt(data[row][7]); int hw4 = Integer.parseInt(data[row][8]); int hw5 = Integer.parseInt(data[row][9]); int hw6 = Integer.parseInt(data[row][10]); int hw7 = Integer.parseInt(data[row][11]); int HW = (hw1+hw2+hw3+hw4+hw5+hw6+hw7)/7; String HWav = Integer.toString(HW); int[] trying = {Test, HW}; int low = find_min(trying); String Lows = Integer.toString(low); String grade = null; if(low>=90) { grade ="A"; } if(low < 90 && low>= 80) { grade = "B"; } if(low <80 && low>=70) { grade ="C"; } if(low<70 && low>=60) { grade="D"; } if(low<60) { grade = "F"; } test_avg.add(Testav); HW_avg.add(HWav); NAME.add(Name); Hell.add(Name); Hell.add(Testav); Hell.add(HWav); Hell.add(Lows); Hell.add(grade); } System.out.println(Hell); System.out.printf("\n"); File csvFile = new File(csv_name); try (PrintWriter csvWriter = new PrintWriter(new FileWriter(csvFile));){ Hell.stream().forEach(csvWriter::println); } catch (IOException e) { } } public static int find_min(int[] values) { int result = values[0]; for(int i = 0; i<values.length; i++) { if(values[i]<result) { result = values[i]; } } return result; } public static boolean sanityCheck(String[][] data) { if (data == null) { System.out.printf("Sanity check: nul data\n"); return false; } if(data.length<3) { System.out.printf("Sanity check: %d rows\n",data.length); return false; } int cols= data[0].length; for(int row = 0; row<data.length; row++) { int current_cols = data[row].length; if(current_cols!=cols) { System.out.printf("Sanity Check: %d columns at rows%d\n", current_cols, row); return false; } } return true; } public static String[][] readSpreadsheet(String filename) { ArrayList<String> lines = readFile(filename); if (lines == null) { return null; } int rows = lines.size(); String[][] result = new String[rows][]; for (int i = 0; i < rows; i++) { String line = lines.get(i); String[] values = line.split(","); result[i] = values; } return result; } public static ArrayList<String> readFile(String filename) { File temp = new File(filename); Scanner input_file; try { input_file = new Scanner(temp); } catch (Exception e) { System.out.printf("Failed to open file %s\n", filename); return null; } ArrayList<String> result = new ArrayList<String>(); while (input_file.hasNextLine()) { String line = input_file.nextLine(); result.add(line); } input_file.close(); return result; } } input file: First,Last,Exam1,Exam2,Final,H1,H2,H3,H4,H5,H6,H7 Ping,Milledge,43,59,68,69,62,43,60,38,37,40 Elisa,Oltz,76,94,73,100,99,100,90,97,100,92 Leonard,Havers,67,95,57,69,95,71,68,61,93,61 Setsuko,Lovera,78,100,84,89,88,92,65,85,66,97 Franklyn,Degnim,54,74,50,63,78,42,42,41,67,64 Gwyneth,Marsico,61,89,81,59,59,62,88,60,66,66 Abigail,Greep,69,99,93,94,91,85,78,91,69,71 Majorie,Granvold,78,100,100,82,96,100,89,100,100,94 Daphine,Polaco,62,82,88,81,68,89,62,73,90,62 An,Corvera,44,71,37,46,57,42,59,66,54,60 Ayanna,Pensiero,64,42,56,37,53,66,69,52,43,58 Era,Deming,98,81,100,69,65,73,77,78,73,89 Michal,Slentz,73,85,81,82,74,93,81,76,69,81 Corie,Brazen,86,99,66,100,69,97,96,100,70,84 Dona,Tufte,63,54,70,71,55,68,86,66,75,63 Juan,Rohdenburg,78,89,100,91,80,97,92,100,98,100 Orville,Samit,88,63,60,88,81,56,91,76,77,80 Ricky,Knoechel,100,100,93,81,100,90,100,92,100,84 Blythe,Threet,38,68,35,61,63,51,48,72,49,51 Sammie,Wachs,46,53,52,76,50,52,56,68,46,75 Estelle,Veazey,72,87,69,98,96,77,95,91,100,91 Agatha,Keckler,100,92,90,95,85,100,94,85,92,100 Novella,Oros,85,76,100,92,84,77,77,90,86,98 Tanya,Quinlisk,47,78,71,50,79,52,69,66,51,45 Marion,Coltrin,68,68,54,39,61,44,66,58,47,74 Helene,Karow,100,100,75,79,100,100,100,92,89,96 Shonta,Bourek,100,96,90,81,97,84,91,100,100,100 Hyon,Anglemyer,81,76,43,43,47,53,44,60,57,65 Ervin,Kenison,78,53,54,75,55,46,61,75,56,69 Renato,Urch,71,64,64,84,49,57,63,69,81,64 Mikel,Burleigh,88,100,90,100,90,91,90,80,74,74 Val,Royal,100,80,100,99,100,100,76,86,100,96 Jodie,Adolfo,94,77,59,83,67,79,87,82,82,75 Roselee,Lienhard,68,75,58,82,96,62,60,94,68,58 Austin,Holznecht,76,49,79,48,58,68,67,71,70,61 Emelia,Toney,70,95,74,90,99,68,100,66,98,98 Lucy,Rhodd,71,91,100,82,100,93,100,100,71,81 Sacha,Chee,78,71,90,82,74,64,62,87,69,84 Julio,Lackner,56,86,53,88,88,73,57,59,80,85 Salvador,Gretzner,54,83,91,66,78,67,61,84,82,6 export file(csv_name) name,exam_score,hw_score,min_score,grade Ping Milledge,56.666667,49.857143,49.857143,F Elisa Oltz,81.000000,96.857143,81.000000,B Leonard Havers,73.000000,74.000000,73.000000,C Setsuko Lovera,87.333333,83.142857,83.142857,B Franklyn Degnim,59.333333,56.714286,56.714286,F Gwyneth Marsico,77.000000,65.714286,65.714286,D Abigail Greep,87.000000,82.714286,82.714286,B Majorie Granvold,92.666667,94.428571,92.666667,A Daphine Polaco,77.333333,75.000000,75.000000,C An Corvera,50.666667,54.857143,50.666667,F Ayanna Pensiero,54.000000,54.000000,54.000000,F Era Deming,93.000000,74.857143,74.857143,C Michal Slentz,79.666667,79.428571,79.428571,C Corie Brazen,83.666667,88.000000,83.666667,B Dona Tufte,62.333333,69.142857,62.333333,D Juan Rohdenburg,89.000000,94.000000,89.000000,B Orville Samit,70.333333,78.428571,70.333333,C Ricky Knoechel,97.666667,92.428571,92.428571,A Blythe Threet,47.000000,56.428571,47.000000,F Sammie Wachs,50.333333,60.428571,50.333333,F Estelle Veazey,76.000000,92.571429,76.000000,C Agatha Keckler,94.000000,93.000000,93.000000,A Novella Oros,87.000000,86.285714,86.285714,B Tanya Quinlisk,65.333333,58.857143,58.857143,F Marion Coltrin,63.333333,55.571429,55.571429,F Helene Karow,91.666667,93.714286,91.666667,A Shonta Bourek,95.333333,93.285714,93.285714,A Hyon Anglemyer,66.666667,52.714286,52.714286,F Ervin Kenison,61.666667,62.428571,61.666667,D Renato Urch,66.333333,66.714286,66.333333,D Mikel Burleigh,92.666667,85.571429,85.571429,B Val Royal,93.333333,93.857143,93.333333,A Jodie Adolfo,76.666667,79.285714,76.666667,C Roselee Lienhard,67.000000,74.285714,67.000000,D Austin Holznecht,68.000000,63.285714,63.285714,D Emelia Toney,79.666667,88.428571,79.666667,C Lucy Rhodd,87.333333,89.571429,87.333333,B Sacha Chee,79.666667,74.571429,74.571429,C Julio Lackner,65.000000,75.714286,65.000000,D Salvador Gretzner,76.000000,71.714286,71.714286,C export file 2(pretty_name) name: exam score, hw score, min score, grade Ping Milledge: 56.67, 49.86, 49.86, F Elisa Oltz: 81.00, 96.86, 81.00, B Leonard Havers: 73.00, 74.00, 73.00, C Setsuko Lovera: 87.33, 83.14, 83.14, B Franklyn Degnim: 59.33, 56.71, 56.71, F Gwyneth Marsico: 77.00, 65.71, 65.71, D Abigail Greep: 87.00, 82.71, 82.71, B Majorie Granvold: 92.67, 94.43, 92.67, A Daphine Polaco: 77.33, 75.00, 75.00, C An Corvera: 50.67, 54.86, 50.67, F Ayanna Pensiero: 54.00, 54.00, 54.00, F Era Deming: 93.00, 74.86, 74.86, C Michal Slentz: 79.67, 79.43, 79.43, C Corie Brazen: 83.67, 88.00, 83.67, B Dona Tufte: 62.33, 69.14, 62.33, D Juan Rohdenburg: 89.00, 94.00, 89.00, B Orville Samit: 70.33, 78.43, 70.33, C Ricky Knoechel: 97.67, 92.43, 92.43, A Blythe Threet: 47.00, 56.43, 47.00, F Sammie Wachs: 50.33, 60.43, 50.33, F Estelle Veazey: 76.00, 92.57, 76.00, C Agatha Keckler: 94.00, 93.00, 93.00, A Novella Oros: 87.00, 86.29, 86.29, B Tanya Quinlisk: 65.33, 58.86, 58.86, F Marion Coltrin: 63.33, 55.57, 55.57, F Helene Karow: 91.67, 93.71, 91.67, A Shonta Bourek: 95.33, 93.29, 93.29, A Hyon Anglemyer: 66.67, 52.71, 52.71, F Ervin Kenison: 61.67, 62.43, 61.67, D Renato Urch: 66.33, 66.71, 66.33, D Mikel Burleigh: 92.67, 85.57, 85.57, B Val Royal: 93.33, 93.86, 93.33, A Jodie Adolfo: 76.67, 79.29, 76.67, C Roselee Lienhard: 67.00, 74.29, 67.00, D Austin Holznecht: 68.00, 63.29, 63.29, D Emelia Toney: 79.67, 88.43, 79.67, C Lucy Rhodd: 87.33, 89.57, 87.33, B Sacha Chee: 79.67, 74.57, 74.57, C Julio Lackner: 65.00, 75.71, 65.00, D Salvador Gretzner: 76.00, 71.71, 71.71, C
This should do it, if you have question about the code, just ask. public static void processGrades (String input_name, String csv_name, String pretty_name) { DecimalFormat decimalFormat = new DecimalFormat(".000000"); String[][] data = readSpreadsheet(input_name); String[][] result = new String[data.length][]; result[0] = new String[]{"name", "exam_score", "hw_score", "min_score", "grade"}; // Export to 2D String array for(int row = 1; row < data.length; row++) { String name = data[row][0] + " " + data[row][1]; double testAverage = average(data[row], 2, 5); double homeworkAverage = average(data[row], 5, 12); double min = Math.min(testAverage, homeworkAverage); char grade = (char) (74 - ((int) min / 10)); grade = grade > 'D' ? 'F' : grade; result[row] = new String[]{ name, decimalFormat.format(testAverage), decimalFormat.format(homeworkAverage), decimalFormat.format(min), Character.toString(grade) }; } // Export 2D array into a csv String String csv = ""; for (int y = 0; y < result.length; y++) { for (int x = 0; x < result[y].length - 1; x++) { csv += result[y][x] + ","; } csv += result[y][result[y].length - 1] + "\n"; } // Save String in file File file = new File(csv_name); try { BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile())); bw.write(csv); bw.close(); } catch (IOException e) { e.printStackTrace(); } } private static double average(String[] row, int fromIndex, int toIndex) { double total = 0; for (int i = fromIndex; i < toIndex; i++) { total += Integer.parseInt(row[i]); } return total / (toIndex - fromIndex); }
How do I export the ArrayList to a CSV file
I have this code, it gets the average grade, but I need to export the arraylist Hell to a CSV file. How do I do this? import java.util.*; import java.io.*; import java.io.PrintWriter; import java.text.*; public class hello3 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter the name of the input file: "); String input_name = in.next(); System.out.printf("Please enter the name of the output CSV file: "); String csv_name = in.next(); System.out.printf("Please enter the name of the output pretty-print file: "); String pretty_name = in.next(); processGrades(input_name, csv_name, pretty_name); System.out.printf("\nExiting...\n"); } public static void processGrades (String input_name, String csv_name, String pretty_name) { PrintWriter csv = null; PrintWriter pretty = null; String[][] data = readSpreadsheet(input_name); boolean resultb = sanityCheck(data); int length = data.length; ArrayList<String> test_avg = new ArrayList<String>(); ArrayList<String> HW_avg = new ArrayList<String>(); ArrayList<String> NAME = new ArrayList<String>(); ArrayList<String> ColN = new ArrayList<String>(); ArrayList<String> Hell = new ArrayList<String>(); for(int row = 1; row<length; row++) { String name = data[row][0]; String name2 = data[row][1]; String Name = name+" "+name2; int test1 = Integer.parseInt(data[row][2]); int test2 = Integer.parseInt(data[row][3]); int test3 = Integer.parseInt(data[row][4]); int Test = (test1+test2+test3)/3; String Testav = Integer.toString(Test); int hw1 = Integer.parseInt(data[row][5]); int hw2 = Integer.parseInt(data[row][6]); int hw3 = Integer.parseInt(data[row][7]); int hw4 = Integer.parseInt(data[row][8]); int hw5 = Integer.parseInt(data[row][9]); int hw6 = Integer.parseInt(data[row][10]); int hw7 = Integer.parseInt(data[row][11]); int HW = (hw1+hw2+hw3+hw4+hw5+hw6+hw7)/7; int[] trying = {Test, HW}; int low = find_min(trying); String grade = null; if(low>=90) { grade ="A"; } if(low < 90&& low>= 80) { grade = "B"; } if(low <80&&low>=70) { grade ="C"; } if(low<70&&low>=60) { grade="D"; } if(low<60) { grade = "F"; } String Lows = Integer.toString(low); String HWav = Integer.toString(HW); test_avg.add(Testav); HW_avg.add(HWav); NAME.add(Name); Hell.add(Name); Hell.add(Testav); Hell.add(HWav); Hell.add(Lows); Hell.add(grade); System.out.println(Hell); System.out.printf("\n"); } } public static int find_min(int[] values) { int result = values[0]; for(int i = 0; i<values.length; i++) { if(values[i]<result) { result = values[i]; } } return result; } public static boolean sanityCheck(String[][] data) { if (data == null) { System.out.printf("Sanity check: nul data\n"); return false; } if(data.length<3) { System.out.printf("Sanity check: %d rows\n",data.length); return false; } int cols= data[0].length; for(int row = 0; row<data.length; row++) { int current_cols = data[row].length; if(current_cols!=cols) { System.out.printf("Sanity Check: %d columns at rows%d\n", current_cols, row); return false; } } return true; } public static String[][] readSpreadsheet(String filename) { ArrayList<String> lines = readFile(filename); if (lines == null) { return null; } int rows = lines.size(); String[][] result = new String[rows][]; for (int i = 0; i < rows; i++) { String line = lines.get(i); String[] values = line.split(","); result[i] = values; } return result; } public static ArrayList<String> readFile(String filename) { File temp = new File(filename); Scanner input_file; try { input_file = new Scanner(temp); } catch (Exception e) { System.out.printf("Failed to open file %s\n", filename); return null; } ArrayList<String> result = new ArrayList<String>(); while (input_file.hasNextLine()) { String line = input_file.nextLine(); result.add(line); } input_file.close(); return result; } } Any help would be appreciated. thank you.
Broadly, you need to open a file with the name you require, and a writer in a loop - like this: File csvFile = new File(csvName); try (PrintWriter csvWriter = new PrintWriter(new FileWriter(csvFile));){ for(String item : list){ csvWriter.println(item); } } catch (IOException e) { //Handle exception e.printStackTrace(); } Obviously you will have to print some commas as required