How to fill an arraylist with an information from an array (java) -
i trying create program reads txt file (this thing in file "5,5,5,0"). want take information, put in array, use array fill array list. use arraylist write infromation file.
here have far in class file:
import java.io.*; import java.util.scanner; import java.util.arraylist; public void setmoney() throws ioexception { file moneyfile = new file ("money.txt"); scanner moneyscan = new scanner(moneyfile); string [] tokens = moneyfile.split(","); arraylist<integer> money = new arraylist<integer>(arrays.aslist(tokens)); for(int i=0;i<tokens.length;i++){ money.append(tokens[i]); } string s = integer.tostring(tokens[i]); fileoutputstream fos = new fileoutputstream("money.txt"); fos.write(money); fos.close(); }
money.append
giving me error:
error: cannot find symbol money.append(tokens[i]); ^
symbol: method append(string) location: variable money of type arraylist
moneyfile.split
giving me error:
error: cannot find symbol string [] tokens = moneyfile.split(","); ^ symbol: method split(string) location: variable moneyfile of type file
you have use fileinputstream
instead of file
. also, use scanner
object create in order int
values:
fileinputstream moneyfile = new fileinputstream("path/money.txt"); scanner moneyscan = new scanner(moneyfile); moneyscan.usedelimiter(","); arraylist<integer> money = new arraylist<integer>(); while(moneyscan.hasnextint()) money.add(moneyscan.nextint());
Comments
Post a Comment