Solved

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

Posted on 2004-09-07
6
227 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
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 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