I am creating a function to load in a text file, the problem is that instead of what you would expect from a text file format (name|id|address|etc) it is presented like this;
Obe Two Canobi
Specialist
false
JK001
125
F010
on task
Programming and Computer Support
Down a text file, My program must be able to load any amount of this group of 8. Here is what I have come up with but doesnt work for whatever reason, it will re-load the same lines from the first input from what ive printed.
//Custom function to load files from given inputs
public static void loadFile(String location,String file,String type) {
//Using try/catch to retrieve errors
WorkUnit[] wu = null;
try {
//Using the buffered reader to load the file.
BufferedReader in = null;
try {
LineNumberReader lnr = new LineNumberReader(new FileReader(new File("C:\\Users\\kenji\\IdeaProjects\\Assingment01C_LaTrobe\\src\\workunit.txt")));
lnr.skip(Long.MAX_VALUE);
int length = lnr.getLineNumber() + 1;
print(length + "");
lnr.close();
in = new BufferedReader(new FileReader( "C:\\Users\\kenji\\IdeaProjects\\Assingment01C_LaTrobe\\src\\workunit.txt" ));
wu = new WorkUnit[length/8];
print(wu.length + "");
String currentLine;
for(int i=0;i<length/8;i+=8) {
Crew cr = new Crew();
Ship ship = new Ship();
cr.setName(in.readLine());
cr.setClassification(getCrewClassification(in.readLine()));
cr.setStatus(getTrueFalse(in.readLine()));
cr.setId(in.readLine());
cr.setPoints(Integer.parseInt(in.readLine()));
ship.setId(in.readLine());
ship.setShipStatus(getShipStatus(in.readLine()));
ship.setPurpose(in.readLine());
wu[i] = new WorkUnit(ship,cr);
print(wu[i].toString());
}
} finally {
in.close();
}
} catch (Exception ex) { ex.printStackTrace(); }
for(int i = 0; i < wu.length;i++) {
print(wu[i].toString());
}
}
Here is the output
16
2
WorkUnit{ship=Ship{id='F010', purpose='Programming and Computer Support', shipStatus=on_task}, crew=Crew{id='JK001', name='Obe Two Canobi', experiencePoints=125, status=false, classification=Specialised}, available=true}
WorkUnit{ship=Ship{id='F010', purpose='Programming and Computer Support', shipStatus=on_task}, crew=Crew{id='JK001', name='Obe Two Canobi', experiencePoints=125, status=false, classification=Specialised}, available=true}
Exception in thread "main" java.lang.NullPointerException
at SpaceCity.loadFile(SpaceCity.java:131)
at SpaceCity.addMenuChoice(SpaceCity.java:81)
at SpaceCity.MainMenu(SpaceCity.java:32)
at SpaceCity.main(SpaceCity.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Process finished with exit code 1
List wu has 2 spaces, But i hops by eight. change the code
for(int i=0;i<length/8;i+=8) {
to
for(int i=0;i<length/8;i++) {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.