1

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
Bill
  • 4,477
  • 5
  • 28
  • 49
Ty Givens
  • 111
  • 2
  • 11

2 Answers2

2

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();
FThompson
  • 27,043
  • 11
  • 53
  • 89
Sami Eltamawy
  • 9,350
  • 8
  • 46
  • 66
  • I think you might have misread the question. The goal isn't to convert an `int` into a string, nor to write a data to a file. And OP's code already constructs an `ArrayList` from the array. – FThompson May 04 '13 at 20:28
  • Thats why you have voted me down!!? May be I have misunderstood the question, but I think I am not that far from the requirement and I know that I wanted to help him. Finally, thanks for your down vote @Vulcan – Sami Eltamawy May 04 '13 at 20:35
  • I added this to my file but now I'm getting an illegal start of type error. – Ty Givens May 04 '13 at 20:36
  • @TyGivens, Which statement exactly that gives your the error? – Sami Eltamawy May 04 '13 at 20:37
2

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());
Philip
  • 253
  • 1
  • 15
  • the `FileInputStream` is unnecessary; scanners can read from files just fine. However, the rest of this is correct. – jedyobidan May 04 '13 at 22:10
  • @jedyobidan [Really?](http://stackoverflow.com/questions/9492520/java-scannerfile-misbehaving-but-scannerfileinputstream-always-works-with-t) – Philip May 04 '13 at 22:14
  • I am assuming the OP is using a standard ASCII charset (as he stated, the only thing in his file are numbers and commas). If it were not in ASCII, I suppose FileInputStream would work, although so would specifying the charset in the constructor – jedyobidan May 04 '13 at 22:19