473,574 Members | 17,993 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 14374
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
5083
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...
3
3750
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
6846
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...
0
1480
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...
7
2513
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
1996
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...
0
1242
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...
1
16457
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
9755
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
7738
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...
1
7835
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...
0
8121
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...
0
6486
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5635
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...
0
3759
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2255
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
1
1360
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1084
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...

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.