Organizing imported text file, char or strings? -
i have text file reads this:
the 3333(etc.) students id number, , 3 numbers after his/hers test scores. 0.3 weight first 2 test scores, 0.4 weight third. 0 separate classes.
i have buffered reader input data, im not sure next. convert character array? or save each # int , use math functions.
all have right read file.
import java.io.bufferedreader; import java.io.filereader; import java.io.ioexception; public class jose_coursedata { public static void main(string[]args) { try (bufferedreader br = new bufferedreader(new filereader("c:\\users\\jose\\documents\\loops\\coursedata.txt"))) { string scurrentline; while ((scurrentline = br.readline()) != null) { system.out.println(scurrentline); } } catch (ioexception e) { e.printstacktrace(); } }
text file looks
0.30 0.30 0.40 161 3333 70 60 50 4444 50 50 50 5555 80 90 80 0 162 1212 90 85 92 6666 60 80 90 7777 90 90 90 8888 95 87 93 9999 75 77 73 0 263 2222 90 65 75 8989 60 40 60 9090 70 80 30 0
i need take text, organize it, , average scores. here's should like
grade data class 161 id programs midterm final weighted average programs grade -- -------- ------- ----- ---------------- -------------- 3333 70 60 50 59.00 pass 4444 50 50 50 50.00 fail 5555 80 90 80 83.00 pass class average: 64.00 grade data class 162 id programs midterm final weighted average programs grade -- -------- ------- ----- ---------------- -------------- 1212 90 85 92 ... pass 6666 60 80 90 ... fail 7777 90 90 90 ... pass 8888 95 87 93 ... pass 9999 75 77 73 ... pass class average: ... grade data class 263 id programs midterm final weighted average programs grade -- -------- ------- ----- ---------------- -------------- 2222 . . . 8989 . . . 9090 . . . class average: ...
you can use java.nio obtain contents of file string list can filter lines 1 1. list class has toarray(t[]a) method use convert string array. can filter through lines work out 1 use, , how print it. example:
package testers; import java.io.file; import java.io.ioexception; import java.nio.file.files; import java.nio.file.path; import java.nio.file.paths; import java.util.list; public class testclassresults{ public static int testone = 30; //percentages. marked integers. these can changed public static int testtwo = 30; public static int testthree = 40; public static list<float> average; public static int students; public static string[] getasarray(list<string>ls ){ return ls.toarray(new string[]{}); } public static void main(string[] args){ file file = new file("c:\\users\\jose\\documents\\loops\\coursedata.txt"); path path = paths.get(file.touri()); try { list<string> ls = files.readalllines(path); string[] array = getasarray(ls); parseclassrecord(array[1]); parsestudentrecord(array[2]); parsestudentrecord(array[3]); parseclassaverage(); //you can keep parsing class , student records match whatever need } catch (ioexception e) { e.printstacktrace(); } } public static void parseclassrecord(string classno){ system.out.println("grade data class " + classno); system.out.println("id programs midterm final weighted average programs grade"); system.out.println("-- -------- ------- ----- ---------------- --------------"); } public static void parsestudentrecord(string studentfacts){ system.out.print(studentfacts + " "); string first = studentfacts.substring(6, 7); string second = studentfacts.substring(9, 10); string third = studentfacts.substring(12, 13); int 1 = integer.parseint(first); int 2 = integer.parseint(second); int 3 = integer.parseint(third); float = one/testone; float b = two/testtwo; float c = three/testthree; float percent = (a + b + c) /3; system.out.print(percent + " "); if(percent > 0.5){ //if student scored more 50% in test system.out.print("pass"); }else{ // if didn't system.out.print("fail"); } system.out.println(); average.add(float.valueof(percent)); students = students + 1; // can use students++ (it same thing). } public static void parseclassaverage(){ float total = 0; for(int = 0; < average.size(); i++){ total = total + average.get(i); } float avg = total / students; system.out.println("class average: " + avg); system.out.println(); // creates space between lines. } }
my method, you'll notice, doesn't use bufferedreader, because find simple reading single line, multiple-line file, find easier have them list, since list , collection apis have special methods.
Comments
Post a Comment