io - Reading different variables from text file in Java -


so have text this:

16783 bob arum 30.5 10.00 

a lot of these on different lines in text file (long,string,string,double,double) want store these variables inside of array , far have:

public class main {  public static void main(string[] args){      arraylist<string> arremployee = new arraylist<string>(); // create array employees      try {         scanner txtin = new scanner(new file("payroll.txt"));     }      catch (filenotfoundexception e) {      }  }} 

the problem i'm having though can't figure out way store these values inside of arremployeearray in way use them later. i've figured out far creating different class constructor help, i'm having hard time understanding how access objects in array.

for example, if wanted double @ end of line, it's object stored inside array now, how access specific double?

creating employee class helpful, lets make employee several instance variables. if store last double 1 of instance variables in loop, retrieve value later calling object.

if choose use way, have change arraylist hold employees instead, , have change how input file.

so if have employee class constructor accepting (long,string,string,double,double) , last double variable named 'd3', use this:

public class test {  public static void main(string[] args) {      arraylist<employee> arremployee = new arraylist<employee>(); // create array employees      try {         scanner txtin = new scanner(new file("payroll.txt"));         while (txtin.hasnext()) {             double d1 = txtin.nextdouble();             string s1 = txtin.next();             string s2 = txtin.next();             double d2 = txtin.nextdouble();             double d3 = txtin.nextdouble();             arremployee.add(new employee(d1,s1,s2,d2,d3));         }     } catch (filenotfoundexception e) {      }     system.out.println(arremployee.get(0).getd2());//note here how can use method getd2() on method get(0) of arraylist, if list's type employee , you've implemented getd2() return last double  } 

}


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -