class - Creating Objects from input file -
i'm trying somehow create objects reading text file. however, seem doing wrong, , can't put finger on it.
main:
import java.util.*; import java.io.*; public class project2 { public static void main(string[] args) throws ioexception { scanner sc = new scanner((new file("project2datafile.txt"))); sc.usedelimiter(","); arraylist<baseballplayer> myplayer = new arraylist<baseballplayer>(); while (sc.hasnext()) { string str = sc.nextline(); for(int cnt = 0; cnt < 4; cnt++){ baseballplayer player = new baseballplayer(); if( player.batavg < 0 || player.batavg > 100 ){throw new illegalargumentexception ("illegal batting avg");} player.pnumber = sc.nextint(); player.lastname = sc.next(); player.firstname = sc.next(); player.batavg = sc.nextfloat(); } continue; } system.out.println(myplayer); }
class:
public class baseballplayer { public static int pnumber; // player number public static string lastname; // player's last name public static string firstname; // player's first name public static float batavg; // player's batting average }
and might put text file in there too:
48,degrom,jacob,.120 58,mejia,jenry,.140 49,niese,jon,.091 7,d'arnaud,travis,.324 21,duda,lucas,.237 4,flores,wilmner,.268 11,tejada,ruben,.345 5,wright,david,.289 3,granderson,curtis,.327 12,lagares,juan,.298
it seems using static
incorrectly. static
members shared all instances of class, should not use in baseballplayer
. fix problem, need remove static
s in baseballplayer
.
Comments
Post a Comment