473,416 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

How to read data from a file and splitting them into an array list (java)?

Data:

Shoes,(Description in String Type),Air Penny III,Nike,10,100
Shoes,(Description in String Type),Easy Rider III,Puma,9,99

Expand|Select|Wrap|Line Numbers
  1. public static ArrayList[] readFile(String filename){
  2. String line = "";
  3. ArrayList[] data = new ArrayList[5]; //create a new arraylist called data
  4.  
  5. try{
  6. FileReader fr = new FileReader(filename); //create a filereader that read the file by its name
  7. BufferedReader br = new BufferedReader(fr); //create a buffer around the reader
  8. while((line = br.readLine()) != null){ //if the line read is not an "", arraylist will add 1 to its length
  9. String[] theline = line.split(","); //split data by recognising the ","
  10. data[0].add(theline[0]); //storing different categories of info of each row into an array
  11. data[1].add(theline[1]); //e.g. data[3] stores brandname nike, addidas, puma
  12. data[2].add(theline[2]);
  13. data[3].add(theline[3]);
  14. data[4].add(theline[4]);
  15. data[5].add(theline[5]);
  16. }
  17. }
  18. catch(FileNotFoundException e){ //catching possible errors
  19. e.printStackTrace();
  20. }
  21. catch(IOException e){
  22. System.out.println(e);
  23. }
  24. return data; //return the arraylist
  25. }
I then receive an error and I don't know how to solve it.

I would also like to display data[3] into a JList too.

Please help, thanks.
May 24 '10 #1
1 6374
ThatThatGuy
449 Expert 256MB
You can use StringTokenizer class to split the data

String Tokenizer splits a string which is being presented in either as comma separated values or seperated by some symbol


Expand|Select|Wrap|Line Numbers
  1. ArrayList lst=new ArrayList();
  2. StringTokenizer st = new StringTokenizer("<data>", "<seperator symbol>"); 
  3. //check for tokens
  4. if(st.hasMoreTokens()
  5. {
  6. lst.add(st.nextToken());
  7. //adding the data as tokens
  8. }
  9.  
This is how you can split up data and store it in list.

and one more thing
public static ArrayList[] readFile(String filename){
String line = "";
ArrayList[] data = new ArrayList[5]; //create a new arraylist called data
You shouldn't create an array of ArrayList.that's illogical
May 24 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: boarderstu | last post by:
right so im in the midst of creating a gallery for a friend of mine, i have most of it right, but when i get the file names from the directory and into an array i have the ".." and "..." as the...
3
by: xuxf055 | last post by:
Hi Everybody, I am confused for a few days, how to read the data file seperated by comma. for exampe following file. data.txt 1,2,3,4,5,6,7,8,9,10 11,12,13,14,15,16,17,18,19,20 how to read it...
9
JavaStudent07
by: JavaStudent07 | last post by:
I am importing a data file to a sorting method and need to know how to take each line of the imported data and set it equal to an array. EX. 1.) 2 3 4 5 2.) 3 6 2 6 3.) 9 2 5 6 i want...
8
by: hjj | last post by:
Hi every one I have an Assignment which due to tomorrow 6:00 p.m GMT; while (and I am new in programming with C so){ I need a lot of help; } the required program is as following:
8
by: Johnny | last post by:
Hi all: I have an ASP.NET form that reads an Excel file and populates a datagrid. If I load the form in IE on the server, and select a local file, the code works fine. However if I load the form...
3
by: Rupali12345 | last post by:
hi all I m trying to read an .dcm file using byte array.But i dont know actually how to read byte code from file.please give me any example of reading byte code. I search in google also...but in...
4
by: nzjohnc | last post by:
Hi all, just started c ,need some help for storing values in arrays.I have opened the file for reading using fscanf,cos its got only three fields,but when i try to get the values to array,I'm...
22
by: bela | last post by:
Hello, I am very new to JAVA. I would like to know, how to read *.csv file from java and how to save that data into an array. regards, bela
3
by: sasiee2k | last post by:
Dear All, I have a text file. I want use the c#.net to read the file and place an 'A' at the end of every line if the line was read. And place the data in the sql server database. regards, sasi
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.