java - having a for-loop inside a while-loop to read specifically from file? -
i have text file has multiple teams. teams have different variables different values. towards end of line, teams have 2 team members , have max(which 8)
i have while-loop read through line , assign values constructors objects. think should use for-loop assign team members, write in condition assign object of team?
try { while(input.hasnext()) { input.nextline(); 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(); //for loop? input.nextline(); } } catch (nosuchelementexception statexception) { system.out.print("wrong elemt"); } catch (illegalstateexception stateexception) { system.out.print("wrong state"); }
text file in case needs understand question:
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,,,,,, 7079,sportbots,karim,kramer,brian,torres santos,,harmony school of nature & athletics,steven,castillo baca,john,mcgaughey,warren,aktas,diane,barrera,rebeca,escamilla,bert,eickstead,jina,castillejo,eddy,romeo
check below code. @ end of each iteration of while loop, you'll new "newteam" object, can manipulate in whatever way want. each team object contains arraylist hold team members objects. each team member object has firstname , lastname.
i have extended member class storing coach , mentor names, didn't want confuse you. hence, i've kept simple. can build upon this.
import java.io.bufferedreader; import java.io.file; import java.io.filereader; import java.io.ioexception; import java.util.arraylist; public class teamreader { // nested class holding properties of team static class team{ // nested class holding first , last names of members static class member{ string firstname, lastname; public member(string fname, string lname){ this.firstname = fname; this.lastname = lname; } } int id; string teamname, coachfirst, coachlast, mentorfirst, mentorlast, sponsor, org; arraylist<member> members = new arraylist<member>(); public string tostring(){ return integer.tostring(id)+" - "+teamname; } } public static void main(string[] args) throws ioexception { //system.out.println(system.getproperty("user.dir")); finding current working directory file file = new file("input.csv"); bufferedreader br = new bufferedreader(new filereader(file)); string current; current = br.readline(); // skipping csv header while((current = br.readline())!=null){ system.out.println(current); string[] data = current.split(","); team newteam = new team(); (int = 0; < data.length; i++){ switch(i){ case 0: newteam.id = integer.parseint(data[i]); break; case 1: newteam.teamname = data[i]; break; case 2: newteam.coachfirst = data[i]; break; case 3: newteam.coachlast = data[i]; break; case 4: newteam.mentorfirst = data[i]; break; case 5: newteam.mentorlast = data[i]; break; case 6: newteam.sponsor = data[i]; break; case 7: newteam.org = data[i]; break; default:newteam.members.add(new team.member(data[i],data[i+1])); i++; break; } } // whatever want team system.out.println(newteam); } } }
Comments
Post a Comment