Solved

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

Posted on 2004-09-07
6
193 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now