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
3
  • 3
    It appears you have answered your own question, except you have to parse the String to turn them into int values. Do you have another question? Commented May 4, 2013 at 20:07
  • I agree with you Peter Commented May 4, 2013 at 20:10
  • @Ty Givens Arrays.asList ? ... Commented May 4, 2013 at 22:13

2 Answers 2

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());
Sign up to request clarification or add additional context in comments.

3 Comments

the FileInputStream is unnecessary; scanners can read from files just fine. However, the rest of this is correct.
@jedyobidan Really?
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
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();

4 Comments

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.
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
I added this to my file but now I'm getting an illegal start of type error.
@TyGivens, Which statement exactly that gives your the error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.