Reputation: 111
I am trying to create a program that reads from a txt file (this is the only thing in the file "5,5,5,0"). Then I want to take that information, put it in an array, then use that array to fill an array list. Then use that arraylist to write infromation into the file.
Here is what I have so far in my 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
is giving me this error:
error: cannot find symbol
money.append(tokens[i]);
^
symbol: method append(String) location: variable money of type ArrayList
moneyFile.split
is giving me this error:
error: cannot find symbol
String [] tokens = moneyFile.split(",");
^
symbol: method split(String)
location: variable moneyFile of type File
Upvotes: 1
Views: 1031
Reputation: 9999
There are many ways to copy your data from Array to ArrayList:
The simplest one:
for (int i = 0; i < tokens.length; i++){
money.add(tokens[i]);
}
To parse your data to String
String s = Integer.toString(tokens[i]);
To write your data into a File:
FileOutputStream fos = new FileOutputStream(path_filename_extension);
fos.write(money);
fos.close();
Upvotes: 2
Reputation: 281
You have to use FileInputStream
instead of File
. Also, use the Scanner
object you create in order to get the 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());
Upvotes: 2