Solved

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

Posted on 2004-09-07
6
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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