Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-09-07
6
Medium Priority
?
236 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 1000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

610 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