Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8266895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:19:34+00:00 2026-06-08T05:19:34+00:00

I want to convert a string that I get from a file, to an

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:19:36+00:00Added an answer on June 8, 2026 at 5:19 am

    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]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that looks like this: 9/1/2009. I want to convert it
I have an ArrayList<JCheckBox> that i want to convert to an ArrayList<String> First I
I have string that displays UTF-8 encoded characters, and I want to convert it
I want to convert the items to a String array or the type that
I have a binary that I want to convert to standard MIME-compliant base64 string.
I have the following string: 2010-04-08T12:46:43+00:00 I want to convert that to: 8th April
I have the string: i am a bad boy. I want to convert that
I have a string. s = '1989, 1990' I want to convert that to
I want to read some code from a file and give this to a
I have strings like 84, 03 etc. that I want to convert to Date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.