Data:
Shoes,(Description in String Type),Air Penny III,Nike,10,100
Shoes,(Description in String Type),Easy Rider III,Puma,9,99 - public static ArrayList[] readFile(String filename){
-
String line = "";
-
ArrayList[] data = new ArrayList[5]; //create a new arraylist called data
-
-
try{
-
FileReader fr = new FileReader(filename); //create a filereader that read the file by its name
-
BufferedReader br = new BufferedReader(fr); //create a buffer around the reader
-
while((line = br.readLine()) != null){ //if the line read is not an "", arraylist will add 1 to its length
-
String[] theline = line.split(","); //split data by recognising the ","
-
data[0].add(theline[0]); //storing different categories of info of each row into an array
-
data[1].add(theline[1]); //e.g. data[3] stores brandname nike, addidas, puma
-
data[2].add(theline[2]);
-
data[3].add(theline[3]);
-
data[4].add(theline[4]);
-
data[5].add(theline[5]);
-
}
-
}
-
catch(FileNotFoundException e){ //catching possible errors
-
e.printStackTrace();
-
}
-
catch(IOException e){
-
System.out.println(e);
-
}
-
return data; //return the arraylist
-
}
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.
1 6374
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 -
ArrayList lst=new ArrayList();
-
StringTokenizer st = new StringTokenizer("<data>", "<seperator symbol>");
-
//check for tokens
-
if(st.hasMoreTokens()
-
{
-
lst.add(st.nextToken());
-
//adding the data as tokens
-
}
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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:
|
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...
|
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...
|
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...
|
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
|
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
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |