473,785 Members | 2,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing an arraylist to a file. CHeck this out!

53 New Member
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 14387
Ganon11
3,652 Recognized Expert Specialist
Are you building firsta correctly? That error sounds like the program thinks there are no elements in firsta.
Oct 4 '07 #2
carlos123
53 New Member
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 Top Contributor
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 Top Contributor
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 ObjectOutputStr eam
And if possible have a look after a Serializable

Enjoy your code.
Good Luck !

Debasis Jana
Oct 5 '07 #5
JosAH
11,448 Recognized Expert MVP
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
carlos123
53 New Member
problem solved. thanks guys
Oct 7 '07 #7

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

Similar topics

9
5102
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 code below): (a)When adding BitArrays to the ArrayList and then looping through the ArrayList I seem to access only the latest added BitArray and I'm not exactly clear on best way to access each BItArray in the ArrayList (b)When I try to remove...
3
3758
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 the middle of building the arraylist and wanted to call the last record added to arraylist object how can I do this? Thanks,
2
6860
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 replace just one item. I know I can get the entire array, then save the whole thing (with a for loop and if statements so that the changed data will be saved), but it seems like a lot of unnecessary reading and writing. Is there a way to directly save...
0
1492
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 RichtextBox.LoadFile( ) i can display it on the first box. Then, in a for loop i read line by line , extract the information and (the idea is) to put the transformed data in another RichText Box so that the
7
2522
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
2012
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 learning and really need some help bad. Public Shared Function GetAll() As ArrayList Dim dsCustomer As New DataSet() Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _ "FROM CustomerTable" Try
0
1263
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, then the datagrid is bound to it, and it works fine. The bottom DataGrid is bound to a different ArrayList that holds the same type as the first ArrayList. Both DataGrids are set to Read-Only. The user selects a row from the top DataGrid, then...
1
16465
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.*; /** * */ public class Bank
12
9789
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 As String End Structure
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10085
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.