Ok guys, check this out! Im getting an error "Error: Index: 0, Size: 0" not sure why. - try{
-
// Create file
-
FileWriter fstream = new FileWriter("database.txt");
-
BufferedWriter out = new BufferedWriter(fstream);
-
-
for( int i=0; i<firsta.size(); i++)
-
{
-
-
out.write("First Name: "+firsta.get(i));
-
out.write("Last Name: "+lasta.get(i));
-
out.write("Age: "+agea.get(i));
-
out.write("Street Address: "+streeta.get(i));
-
out.write("City Name: "+citya.get(i));
-
out.write("State Name: "+statea.get(i));
-
out.write("Year: "+yeara.get(i));
-
out.write("Gender: "+gendera.get(i));
-
-
}
-
//Close the output stream
-
out.close();
-
}catch (Exception e){//Catch exception if any
-
System.out.println("Error: " + e.getMessage());
-
}
6 14367
Are you building firsta correctly? That error sounds like the program thinks there are no elements in firsta.
does that look like the right way to add data to an arraylist? - firststring = first.getText();
-
firsta.add(i,firststring);
-
laststring = last.getText();
-
lasta.add(i,laststring);
-
try{
-
agestring = age.getText();
-
ageint = Integer.parseInt(agestring);
-
}catch (Exception e){//Catch exception if any
-
System.out.println("Error: Numbers only....");
-
}
-
agea.add(i,ageint);
-
streetstring = street.getText();
-
streeta.add(i,streetstring);
-
citystring = city.getText();
-
citya.add(i,citystring);
-
System.out.println(firsta.get(i));
-
i++;
-
does that look like the right way to add data to an arraylist? - firststring = first.getText();
-
firsta.add(i,firststring);
-
laststring = last.getText();
-
lasta.add(i,laststring);
-
try{
-
agestring = age.getText();
-
ageint = Integer.parseInt(agestring);
-
}catch (Exception e){//Catch exception if any
-
System.out.println("Error: Numbers only....");
-
}
-
agea.add(i,ageint);
-
streetstring = street.getText();
-
streeta.add(i,streetstring);
-
citystring = city.getText();
-
citya.add(i,citystring);
-
System.out.println(firsta.get(i));
-
i++;
-
You need not to have here index.
Because add simply add sequentially.
It seems your code is right.
Debasis Jana.
does that look like the right way to add data to an arraylist? - firststring = first.getText();
-
firsta.add(i,firststring);
-
laststring = last.getText();
-
lasta.add(i,laststring);
-
try{
-
agestring = age.getText();
-
ageint = Integer.parseInt(agestring);
-
}catch (Exception e){//Catch exception if any
-
System.out.println("Error: Numbers only....");
-
}
-
agea.add(i,ageint);
-
streetstring = street.getText();
-
streeta.add(i,streetstring);
-
citystring = city.getText();
-
citya.add(i,citystring);
-
System.out.println(firsta.get(i));
-
i++;
-
And how do you write "ArrayList" in your "File"?
It seems something odd :-)
You can have a look at ObjectInutStrem and ObjectOutputStream
And if possible have a look after a Serializable
Enjoy your code.
Good Luck !
Debasis Jana
Are you building firsta correctly? That error sounds like the program thinks there are no elements in firsta.
I think it's the other way around, i.e. firsta isn't empty while one of the other
array lists is empty.
kind regards,
Jos
problem solved. thanks guys
Sign in to post your reply or Sign up for a free account.
Similar topics
by: vbportal |
last post by:
Hi,
I would like to add BitArrays to an ArrayList and then remove any duplicates
- can someone please help me forward. I seem to have (at leaset ;-) )2
problems/lack of understanding (see test...
|
by: JJ |
last post by:
Hi,
I have created an Arraylist object from an Arraylist class.
I added rows to the arraylist object and I need to find a particular record
in my arraylist. How do I do this? Also if I was in...
|
by: melanieab |
last post by:
Hi,
I'm trying to store all of my data into one file (there're about 140 things
to keep track of). I have no problem reading a specific string from the
array file, but I wasn't sure how to...
|
by: steve |
last post by:
Hello,
I have been working on this piece of code that's supposed to read from a
file - transform - preview and finally save the transformed data.
I can read the file and with...
|
by: millenium |
last post by:
Several related questions:
1. Can an ArrayList hold a Byte or a Char ?
why is this not working
Dim al As ArrayList
Dim ch As Char
ch = "c"
al.Add(ch)
|
by: Allen |
last post by:
I have a class that returns an arraylist. How do I fill a list box from what
is returned? It returns customers which is a arraylist but I cant seem to
get the stuff to fill a list box. I just...
|
by: Marcus Kwok |
last post by:
I am having a weird problem with my DataGrid that is bound to an
ArrayList. My situation is as follows:
I have two DataGrids on a modal form. The top grid populates an
ArrayList from a file,...
|
by: jrodri1080 |
last post by:
I have a script that i need help with from school, i have an arraylist and i need to switch that for a HashMap. I would appreciate if anyone can help. THANKS
import java.util.*;
/**
*
*/...
|
by: Justin |
last post by:
Ok, I give up. I can't seem to construct a decent (productive) way of
sorting my arraylist.
I have a structure of two elements:
Structure TabStructure
Dim TabName As String
Dim FullFilePath...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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:
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |