java - No Line found? reading file error? -
i no line found
error reading while loop. when print input.nextline()
says countt == 0. means, while loop not working?
public class readerfile { public static scanner input;
try-catch of input.
try{ input = new scanner(new file(filelocation)).usedelimiter(","); } catch (ioexception ioexception) { system.out.print("problem"); }
this code i'm having problems with.
int countt = 0; input.nextline(); while(input.hasnext()) { system.out.print("teamstart\n"); int id = input.nextint(); string teamname = input.next(); string coachfirst = input.next(); string coachlast = input.next(); string mentorfirst = input.next(); string mentorlast = input.next(); string teamfs = input.next(); string teamss = input.next(); input.nextline(); team team = new team (teamname, id, coachfirst, coachlast,mentorfirst,mentorlast,teamfs,teamss); store.add(team); system.out.print(id); countt = countt+1; //system.out.print("\n"+countt); }
this text file i'm reading
teamnumber,team name,coach first,coach last,mentor first,mentor last,team fin sponsor,schools or sponsoring organization,tmmem1first,tmmem1last,tmmem2first,tmmem2last,tmmem3first,tmmem3last,tmmem4first,tmmem4last,tmmem5first,tmmem5last,tmmem6first,tmmem6last,tmmem7first,tmmem7last,tmmem8first,tmmem8last 6842,reagan ray-guns,judy,mallon,aziz,valdez,texas workforce commission,reagan h s,steven,cepeda,alan,yue,tim,callaway,damon,bertucci,samuel,de olvieira,samuel,day,,,, 6888,islanders,judy,maldonado,brady,trevino,three rivers robotics,three rivers middle,shireen,cowdrey,dee,roundtree,steven,callaway,francisco,bermea,,,,,,,, 7004,greenhill tops,kanat,labass,harvey,pflueger,greenhill boosters,greenhill school,harvey,pflueger,sandra,day,denny,rodriguez,shirley,couvillon,carly,szarka,,,,,,
first, suggest use try-with-resources
statement , please not problem when exception
.
try (scanner input = new scanner(new file(filelocation))) { } catch (ioexception ioexception) { ioexception.printstacktrace(); }
i read line. remember should call hasnext
before attempt read , can split on ,
like
while (input.hasnextline()) { int col = 0; string line = input.nextline(); string[] arr = line.split(","); system.out.print("teamstart\n"); int id = integer.parseint(arr[col++]); string teamname = arr[col++]; string coachfirst = arr[col++]; string coachlast = arr[col++]; string mentorfirst = arr[col++]; string mentorlast = arr[col++]; string teamfs = arr[col++]; string teamss = arr[col]; store.add(new team(teamname, id, coachfirst, coachlast, mentorfirst, mentorlast, teamfs, teamss)); system.out.println(id); }
it appears file starts header ... can add
if (input.hasnextline()) { string hdr = input.nextline(); }
before above while loop skip it.
Comments
Post a Comment