java - Console output and file output seem to be different -
so doing program go through every folder in designated folder , if there files inside of thoes folder, read them.
the files this:
file name: grades mait 5 5 5 kati 3 3 3 vello 4 4 4
so read in each line , 0th element in every line name of student add array. after take grades , calculate median of them.
also files in folders named class name. instance math has every math grade in it.
now programs job read thoes files , produce output file contains every students name , median in every class , after median of grades.
this how output looks right now:
nimi: mait keskmine hinne: 5.0 aine: ..\text\students\english\grades nimi: kati keskmine hinne: 3.0 aine: ..\text\students\english\grades nimi: vello keskmine hinne: 4.0 aine: ..\text\students\english\grades nimi: mait keskmine hinne: 1.0 aine: ..\text\students\math\grades nimi: kati keskmine hinne: 2.0 aine: ..\text\students\math\grades
it's not in english in form of name:, median; , class name;
now problem here that, when print same thing out console, instead of file this:
nimi: mait keskmine hinne: 5.0 aine: ..\text\students\english\grades nimi: kati keskmine hinne: 3.0 aine: ..\text\students\english\grades nimi: vello keskmine hinne: 4.0 aine: ..\text\students\english\grades nimi: mait keskmine hinne: 5.0 aine: ..\text\students\english\grades nimi: kati keskmine hinne: 3.0 aine: ..\text\students\english\grades nimi: vello keskmine hinne: 4.0 aine: ..\text\students\english\grades nimi: mait keskmine hinne: 1.0 aine: ..\text\students\math\grades nimi: kati keskmine hinne: 2.0 aine: ..\text\students\math\grades
now can see, first file read, duplicated or reason. problem not occur when have 1 folder program can read from. more folders.
now why problem me? because cannot see how manage calculate median of grades 1 student has gotten in school, when have duplicate values in program.
what might issue here , how can solve it?
here classes in project guys see code:
main.java:
package files; import java.io.bufferedreader; import java.io.filenotfoundexception; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; import java.io.printwriter; import java.nio.file.files; import java.nio.file.path; import java.nio.file.paths; import java.util.arraylist; import java.util.list; public class main { public static void main(string[] args) throws filenotfoundexception { path lessons = paths.get("../text/students"); filevisitor file = new filevisitor(); try { files.walkfiletree(lessons, file); } catch (ioexception e) { e.printstacktrace(); } list<path> files = file.askfiles(); list<student> students = new arraylist<>(); for(path adress:files){ //system.out.println(adress.getfilename()); try { bufferedreader textin = new bufferedreader(new filereader(adress.tostring())); string line; while((line = textin.readline()) != null){ string[] readcontent = line.split(" "); student student = new student(readcontent[0],adress.tostring()); //system.out.println("sad"); for(int = 1;i < readcontent.length; i++){ student.addgrade(readcontent[i]); //system.out.println(adress.getparent()+ " "); } students.add(student); } printwriter output = new printwriter(new filewriter("../text/opilased_out")); for(student i:students){ output.println("nimi: "+i.askname() + " " + "keskmine hinne: "+i.askmedian() + " " + "aine: "+i.asksubject()); system.out.println("nimi: "+i.askname() + " " + "keskmine hinne: "+i.askmedian() + " " + "aine: "+i.asksubject()); } output.close(); } catch (ioexception e) { e.printstacktrace(); } } } }
student.java:
package files; import java.util.arraylist; import java.util.list; public class student { string studentname; list<integer> studentgrades = new arraylist<>(); string subjectname; double sum; public student(string name, string subject) { subjectname = subject; studentname = name; } public string asksubject(){ return subjectname; } public string askname(){ return studentname; } public void addgrade(string grade) { studentgrades.add(integer.parseint(grade)); //system.out.println(studentgrades); (int = 0;i < studentgrades.size(); i++){ //system.out.println(studentgrades.get(i)); } } public double askmedian() { double median = 0; (int = 0;i < studentgrades.size(); i++){ //system.out.println(studentgrades.get(i)); median += studentgrades.get(i); sum = median/studentgrades.size(); } //system.out.println(studentgrades); //system.out.println(sum); return sum; } }
filevisitor.java:
package files; import java.io.ioexception; import java.nio.file.filevisitresult; import java.nio.file.path; import java.nio.file.simplefilevisitor; import java.nio.file.attribute.basicfileattributes; import java.util.arraylist; import java.util.list; public class filevisitor extends simplefilevisitor<path> { private list<path> adresses = new arraylist<>(); @override public filevisitresult postvisitdirectory(path dir, ioexception exc) throws ioexception { return super.postvisitdirectory(dir, exc); } @override public filevisitresult previsitdirectory(path dir, basicfileattributes attrs) throws ioexception { return super.previsitdirectory(dir, attrs); } @override public filevisitresult visitfile(path file, basicfileattributes attrs) throws ioexception { adresses.add(file); //system.out.println(file); return super.visitfile(file, attrs); } @override public filevisitresult visitfilefailed(path file, ioexception exc) throws ioexception { return super.visitfilefailed(file, exc); } public list<path> askfiles(){ return adresses; } }
any appreciated! have been trying figure out problem days now...
Comments
Post a Comment