3

Suppose there is a file named as SUN.txt

File contains : a,b,dd,ss,

I want to make dynamic array depending upon the number of attributes in file. If ther is a char after comma then array will be of 0-4 i.e of length 5. In the above mentioned case there is no Char which returns 0-3 Array of length 4. I want to read the NULL after comma too.

How do i do that?

Sundhas

1
  • 1
    I don't understand the part "NULL after comma"... Commented Dec 2, 2010 at 7:51

6 Answers 6

4

You should think about

  • Reading the file into a String
  • Splitting the file by separator ','
  • Using a list for adding the characters and convert the list to an array, when the list is filled
Sign up to request clarification or add additional context in comments.

2 Comments

I did spilt it by , and read that all in array issue is that the array comes up with one less index.
@Sundhas, if you're using String.split(",") with each line, you may find the need to pass -1 to the limit parameter, as in String.split(",", -1) in order to get the element with the empty string at the end. See the answer I posted.
3

As Markus said, you want to do something like this..

//Create a buffred reader so that you can read in the file
    BufferedReader reader = new BufferedReader(new FileReader(new File(
            "\\SUN.txt")));
    //The StringBuffer will be used to create a string if your file has multiple lines
    StringBuffer sb = new StringBuffer();
    String line;

    while((line = reader.readLine())!= null)
    {
        sb.append(line);
    }

    //We now split the line on the "," to get a string array of the values
    String [] store = sb.toString().split(",");

I do not quite understand why you would want the NULL after the comma? I am assuming that you mean after the last comma you would like that to be null in your array? I do not quite see the point in that but that is not what the question is.

If that is the case you wont read in a NULL, if after the comma there was a space, you could read that in.

If you would like a NULL you would have to add it in yourself at the end so you could do something like

//Create a buffred reader so that you can read in the file
    BufferedReader reader = new BufferedReader(new FileReader(new File(
            "\\SUN.txt")));
    //Use an arraylist to store the values including nulls
    ArrayList<String> store = new ArrayList<String>();
    String line;
    while((line = reader.readLine())!= null)
    {
        String [] splitLine = line.split(",");
        for(String x : splitLine)
        {
            store.add(line);
        }
        //This tests to see if the last character of the line is , and will add a null into the array list
        if(line.endsWith(","))
            store.add(null);
    }

    String [] storeWithNull = store.toArray();

2 Comments

The answer is fine, but could you please remove the "C:\\"? It's a gratuitous Windows-ism that is not relevant to the question. Maybe I'm only saying this because it's 4 in the morning.
Sorry, removed the C:\\ from the code, it was just a habit from testing and didnt even know i did it :P
1

Well if you want want to simply open the file and store the content in a array of string then

1) open the file into a string

2) split the string using a regex "," http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)

but I'm curious why you can't use a String file directly ?

Comments

0

For your datatructure, use a list of arrays. Each list entry is a line of your textfile, each entry is an array that holds the comma separated values:

List<String[]> data = new ArrayList<String[]>();
String line = readNextLine();  // custom method, to be implemented
while (line != null) {
  data.add(line.split(","));
  line = readNextLine();
}

(assuming, your file contains 1..n lines of comma separated values)


You may want to have it like this:

"a,b,c,d,"  -> {"a", "b", "c", "d", null}

Here's a suggestion how to solve that problem:

List<String[]> data = new ArrayList<String[]>();
String line = readNextLine();  // custom method, to be implemented
while (line != null) {
  String[] values = new String[5];
  String[] pieces = line.split(",");
  for (int i = 0; i<pieces.length; i++)
     values[i] = pieces[i];
  data.add(values);

  line = readNextLine();
}

Comments

0

its seems like a CSV file something like this will work assuming it has 5 lines and 5 values

String [][] value = new String [5][5];
File file = new File("SUN.txt");
BufferedReader br  = new BufferedReader(new FileReader(file));
String line = null;
int row = 0;
int col = 0;

while((line = br.readLine()) != null ){ 
StringTokenizer s = new StringTokenizer(line,",");

    while (s.hasMoreTokens()){
    value[row][col] = s.nextToken();
    col++;
}
col = 0;
row++;
}

i havent tested this code

Comments

0

Read the file, using BufferedReader, one line at the time.

Use split(",", -1) to convert to an array of String[] including also empty strings beyond the last comma as part of your array.

Load the String[] parts into a List.

Comments

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.