Solved

Need a function that opens a file and stores it in a string array

Posted on 2004-09-07
6
217 Views
Last Modified: 2010-03-31
Hi,
I need a function that will read a  csv file of this format:

9, ,1
8220,",1
8221,",1
190,3/4,2



and store it in a multidimension array like below

String[][] anArray = { {"9", " ", "1"},{"8220", "\"", "1"},{"8221", "\"", "1"},{"8216", "\'", "1"},{"8217", "\'", "1"},{"226", "a", "1"},{"188","1/4","2"},{"189","1/2","2"},{"190","3/4","2"},{"13"," ","3"}};



Thanks,
Arthur
0
Comment
Question by:vbguy
  • 4
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12001259
List lines = new ArrayList();
BufferedReader in = new BufferedReader(new FileReader(file));
String line = null;
while (null!=(line=in.readLine())
{
   String[] s = line.split(",");
   lines.add(s);
}
String[][] result = (String[][]) lines.toArray(new String[][0]);
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 12001335
sorry, that should be:

List lines = new ArrayList();
BufferedReader in = new BufferedReader(new FileReader("x.txt"));
String line = null;
while (null!=(line=in.readLine()))
{
   String[] s = line.split(",");
   lines.add(s);
}
String[][] result = (String[][]) lines.toArray(new String[0][]);
0
 

Author Comment

by:vbguy
ID: 12001584
Hi,

I have to use jdk 1.3,  is there something for the split method I could use?

Thanks,
Arthur
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 92

Expert Comment

by:objects
ID: 12001619
Use StringTokenizer instead

0
 
LVL 92

Expert Comment

by:objects
ID: 12001629
StringTokenizer st = new StringTokenizer(line, ",");
String[] s = new String[st.countTokens()];
int index = 0;
while (st.hasMoreTokens())
{
   s[index++] = st.nextToken();
}
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 12008461
Hi vbguy,

Try the following.... Some of my codes are derived from objects

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

public class MeowTest {
   
    public static void main(String[] args) {

      try
      {
            FileReader file = new FileReader ("dave.txt");
            BufferedReader in = new BufferedReader(file);
            String line = "";

            int counter = 0;

            while((line = in.readLine()) != null)
            {
                  StringTokenizer st = new StringTokenizer(line, ",");
                  String[][] s = new String[4][st.countTokens()];

                  int index = 0;
                  while (st.hasMoreTokens())
                  {
                           s[counter][index++] = st.nextToken();      
                  }

                  // Display Function
                  for(int i = 0; i < s[counter].length; i++)
                  {
                        System.out.println(s[counter][i]);
                  }
                  counter++;
            }
      }

      catch(Exception e)
      {
            e.printStackTrace();
      }
    }
   
}


I hope that helps...
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Way to decrease size of apk file 9 86
MySQL  on Tomcat 8 68
type mismatch (Object[] to double[] 4 33
How do I remove an object from a 3 40
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question