3

I want to convert a string that I get from a file, to an arraylist. I tried it this way, but it doesn't work:

import java.io.*;
import java.util.*;

public class Data
{
    static File file = DataSaver.file;
    static List<String> data = new ArrayList<String>(512);
    public static void a() throws Exception
    {
        FileInputStream fis = new FileInputStream(file);
        DataInputStream dis = new DataInputStream(fis);
        BufferedReader reader = new BufferedReader(new InputStreamReader(dis));
        if(!file.exists())
        {
            throw new IOException("Datafile not found.");
        }
        else
        {
            String[] string = reader.readLine().split("$");
            for(int i = 0; i < string.length; i++)
            {
                data.add(string[i]);
            }
        }
        dis.close();
        System.out.println(data.toString()); //for debugging purposes.
    }
}

Ouput: [$testdata1$testdata2$]

Wanted output: [testdata1, testdata2]

File content: $testdata1$testdata2$

Can someone help me?

4
  • Why are you calling an array of String 'string'? Commented Jul 23, 2012 at 18:17
  • Why not? You have a problem with the word 'string'? Commented Jul 23, 2012 at 18:25
  • It's a bad variable name. Java may allow it because it's not case-sensitive with it's classes, but that wouldn't work in C# (where string is an alias for String). Furthermore, it doesn't really describe what's contained in the variable. Commented Jul 23, 2012 at 18:26
  • Please don't use DataInputStream if you want to read text, its more confusing than useful. Commented Aug 15, 2012 at 11:33

4 Answers 4

6

String.split takes a regex and $ is a special char that need to be escaped. Also, the first char is a $ so splitting would end up with an empty first element (you need to remove it somehow, this is one way:

String[] string = reader.readLine().substring(1).split("\\$");

...or:

String[] string = reader.readLine().split("\\$");
for (int i = 0; i < string.length; i++)
    if (!string[i].isEmpty())
        data.add(string[i]);
Sign up to request clarification or add additional context in comments.

1 Comment

No problem @user1546467, glad to have helped! :)
3

1. Use ("\\$") to remove the special meaning of "$".

2. Use Arrays.asList() for the Conversion of Array TO ArrayList

From Java Docs :

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.

This method also provides a convenient way to create a fixed-size list initialized to contain several elements:

eg:

String[] string = reader.readLine().split("\\$");

ArrayList<String> arr = new ArrayList<String>(Arrays.asList(string));

1 Comment

@dacwe Isnt the List from Arrays.asList() directly backed by the array? Which would mean almost no overhead.
1

You need to escape special characters with \\.

Change your split statement like below

String[] string = reader.readLine().split("\\$");

Comments

0

Adding to @dacwe

String[] string = reader.readLine().substring(1).split("\\$");
List<String> data =Arrays.asList(string);

2 Comments

If you want to contribute to his answer, edit it or leave a comment.
Alright, but @user1546467 wanted an ArrayList (asList returns a list backed by the array).

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.