File Read and write of text file using Inner class


/* Do not add a package declaration */
import java.util.*;
import java.io.*;

/* DO NOT CHANGE ANYTHING ABOVE THIS LINE */
/* You may add any imports here, if you wish, but only from the
 standard library */

public class Main {
public static class Student {
public int id;
public String courseName;
public int marks;

/**
* @return the id
*/
public int getId() {
return id;
}

/**
* @param id
*            the id to set
*/
public void setId(int id) {
this.id = id;
}

/**
* @return the courseName
*/
public String getCourseName() {
return courseName;
}

/**
* @param courseName
*            the courseName to set
*/
public void setCourseName(String courseName) {
this.courseName = courseName;
}

/**
* @return the marks
*/
public int getMarks() {
return marks;
}

/**
* @param marks
*            the marks to set
*/
public void setMarks(int marks) {
this.marks = marks;
}
}

/**
*
* @param array
*            array
* @return Map<String,Student>
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Map<String, Student> processData(ArrayList<Student> array) {
Map<String, Student> retVal = new HashMap<String, Student>();
Collections.sort(array, new Comparator() {
public int compare(Object obj1, Object obj2) {
Student e1 = (Student) obj1;
Student e2 = (Student) obj2;
Integer firstId = new Integer(String.valueOf(e1.getId()));
Integer secndId = new Integer(String.valueOf(e2.getId()));
int id = firstId.compareTo(secndId);
int name = e1.getCourseName().compareTo(e2.getCourseName());
if (id != 0) {
return id;
} else if (id == 0 && name != 0) {
return name;
}
return id;
}
});
int countDS = 0, countEng = 0, key = 1;
for (Student student : array) {
// Data Structures and English declare in Constant file
if (student.getCourseName().equalsIgnoreCase("Data Structures")
&& countDS == 0) {
retVal.put(String.valueOf(key), student);
key++;
countDS++;
}
if (student.getCourseName().equalsIgnoreCase("English")
&& countEng == 0) {
retVal.put(String.valueOf(key), student);
key++;
countEng++;
}
}
countDS = 0;
countEng = 0;
key = 1;
return retVal;
}

public static void main(String[] args) {
ArrayList<Student> studentCourseList = new ArrayList<Student>();
String line;
try {
@SuppressWarnings("resource")
Scanner in = new Scanner(new BufferedReader(new FileReader(
"input.txt")));
while (in.hasNextLine()) {
// inputData.add(in.nextLine());
line = in.nextLine();
String[] student = line.split("\\|");
Main.Student stdnt = new Main.Student();
stdnt.setId(Integer.parseInt(student[0]));
stdnt.setCourseName(student[1]);
stdnt.setMarks(Integer.parseInt(student[2]));
studentCourseList.add(stdnt);
}
Map<String, Student> retVal = processData(studentCourseList);
PrintWriter output = new PrintWriter(new BufferedWriter(
new FileWriter("output.txt")));
for (Map.Entry<String, Student> entry : retVal.entrySet()) {
Student st = entry.getValue();
output.write(st.getCourseName() + ": " + st.getMarks());
output.println();
}
// output.println(e.getKey() + ": " + e.getValue());
output.close();
} catch (IOException e) {
System.out.println("IO error in input.txt or output.txt");
}
}
}


Step 2>
text file
.........................................
22|Data Structures|45
23|English|52
22|English|51
26|Data Structures|72
23|Data Structures|61
21|English|81


Previous
Next Post »