Friday, 15 June 2012

java - How do I add an instance of a class without the constructor's parameters? -



java - How do I add an instance of a class without the constructor's parameters? -

i'm trying create fantasy football game draft program. first step i'm having hard time reading info list properly. want scan data, line line. created loop sets each role name, goes on add together people have role until "-----" shows on line. role added list of roles. believe loop right that, however, not know how work person constructor correctly contains rank, name, , origin can't accomplish setdata method in person class. i'm still beginner @ programming, , want know if i'm missing something.

data file

leader

1 superman dc

2 captain america marvel

3 professor x marvel

4 shoveler mystery men

brawn

1 hulk marvel

2 wolverine marvel

3 thing marvel

4 beast marvel

5 thor marvel

6 mr. furious mystery men

7 mr. incredible pixar

...and on

main class import java.util.hashmap; import java.util.list; import java.util.scanner; import java.io.*; import java.util.arraylist; public class fantasyteamdraft { /** * joseph simmons * cps 181 * feb 6, 2013 */ public static void main(string[] args) throws ioexception{ scanner scan = new scanner (system.in); system.out.println("enter name , location of file wanted draft: "); file draftdata = new file (scan.next()); scanner scandata = new scanner(draftdata); list <role> listofroles = new arraylist <role> (); while (scandata.hasnext()) { string line = scandata.nextline(); if (!isinteger (line)) { role role = new role (); role.setrolename(line); string personline = scandata.nextline(); while (isinteger(personline)){ person person = new person(); person.setdata(personline); role.addperson(person); } listofroles.add(role); } } } public static boolean isinteger (string line) { seek { integer.parseint(line.split("\t") [0]); } grab (numberformatexception e) { homecoming false; } homecoming true; }

}

person class import java.util.scanner; public class person { private int rank; private string name; private string origin; public person () { } public person (int rank, string name, string origin) { this.rank = rank; this.name = name; this.origin = origin; } public void setdata (string line) { string [] array = line.split("\t"); this.rank = integer.parseint(array [0]); this.name = array [1]; this.origin = array [2]; }

}

role class import java.util.list; import java.util.scanner; public class role { private string rolename = ""; private list <person> listofpeople; public role (string rolename) { this.rolename = rolename; } public void setrolename (string line) { this.rolename = line; } public string getrolename() { homecoming rolename; } public void addperson (person person) { this.listofpeople.add(person); }

}

in code see

while (isinteger(personline)){ person person = new person(); person.setdata(personline); role.addperson(person); }

this infinite loop if isinteger(personline) evaluates true. perhaps should if instead of while, or need modify personline in each iteration.

java constructor instance-variables

No comments:

Post a Comment