473,396 Members | 2,085 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,396 software developers and data experts.

Writing an arraylist to a file. CHeck this out!

Ok guys, check this out! Im getting an error "Error: Index: 0, Size: 0" not sure why.


Expand|Select|Wrap|Line Numbers
  1.  try{
  2.     // Create file 
  3.     FileWriter fstream = new FileWriter("database.txt");
  4.         BufferedWriter out = new BufferedWriter(fstream);
  5.  
  6.         for( int i=0; i<firsta.size(); i++)
  7.         {
  8.  
  9.     out.write("First Name: "+firsta.get(i));
  10.     out.write("Last Name: "+lasta.get(i));
  11.     out.write("Age: "+agea.get(i));
  12.     out.write("Street Address: "+streeta.get(i));
  13.     out.write("City Name: "+citya.get(i));
  14.     out.write("State Name: "+statea.get(i));
  15.     out.write("Year: "+yeara.get(i));
  16.     out.write("Gender: "+gendera.get(i));
  17.  
  18. }
  19.     //Close the output stream
  20.     out.close();
  21.     }catch (Exception e){//Catch exception if any
  22.       System.out.println("Error: " + e.getMessage());
  23.     }
Oct 4 '07 #1
6 14367
Ganon11
3,652 Expert 2GB
Are you building firsta correctly? That error sounds like the program thinks there are no elements in firsta.
Oct 4 '07 #2
does that look like the right way to add data to an arraylist?


Expand|Select|Wrap|Line Numbers
  1. firststring = first.getText();
  2.             firsta.add(i,firststring);
  3.             laststring = last.getText();
  4.             lasta.add(i,laststring);
  5.              try{
  6.             agestring = age.getText();
  7.             ageint = Integer.parseInt(agestring); 
  8.              }catch (Exception e){//Catch exception if any
  9.       System.out.println("Error: Numbers only....");
  10.     }
  11.             agea.add(i,ageint);
  12.             streetstring = street.getText();
  13.             streeta.add(i,streetstring);
  14.             citystring = city.getText();
  15.             citya.add(i,citystring);
  16.             System.out.println(firsta.get(i));
  17.             i++;
  18.  
Oct 4 '07 #3
dmjpro
2,476 2GB
does that look like the right way to add data to an arraylist?


Expand|Select|Wrap|Line Numbers
  1. firststring = first.getText();
  2.             firsta.add(i,firststring);
  3.             laststring = last.getText();
  4.             lasta.add(i,laststring);
  5.              try{
  6.             agestring = age.getText();
  7.             ageint = Integer.parseInt(agestring); 
  8.              }catch (Exception e){//Catch exception if any
  9.       System.out.println("Error: Numbers only....");
  10.     }
  11.             agea.add(i,ageint);
  12.             streetstring = street.getText();
  13.             streeta.add(i,streetstring);
  14.             citystring = city.getText();
  15.             citya.add(i,citystring);
  16.             System.out.println(firsta.get(i));
  17.             i++;
  18.  
You need not to have here index.
Because add simply add sequentially.
It seems your code is right.

Debasis Jana.
Oct 5 '07 #4
dmjpro
2,476 2GB
does that look like the right way to add data to an arraylist?


Expand|Select|Wrap|Line Numbers
  1. firststring = first.getText();
  2.             firsta.add(i,firststring);
  3.             laststring = last.getText();
  4.             lasta.add(i,laststring);
  5.              try{
  6.             agestring = age.getText();
  7.             ageint = Integer.parseInt(agestring); 
  8.              }catch (Exception e){//Catch exception if any
  9.       System.out.println("Error: Numbers only....");
  10.     }
  11.             agea.add(i,ageint);
  12.             streetstring = street.getText();
  13.             streeta.add(i,streetstring);
  14.             citystring = city.getText();
  15.             citya.add(i,citystring);
  16.             System.out.println(firsta.get(i));
  17.             i++;
  18.  
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
Oct 5 '07 #5
JosAH
11,448 Expert 8TB
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
Oct 5 '07 #6
problem solved. thanks guys
Oct 7 '07 #7

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

Similar topics

9
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...
3
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...
2
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...
0
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...
7
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)
16
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...
0
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,...
1
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.*; /** * */...
12
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...
0
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...
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
0
BarryA
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...
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:
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
tracyyun
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...

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.