469,360 Members | 1,807 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

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 14109
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

Post your reply

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

Similar topics

3 posts views Thread by JJ | last post: by
2 posts views Thread by melanieab | last post: by
reply views Thread by steve | last post: by
7 posts views Thread by millenium | last post: by
reply views Thread by Marcus Kwok | last post: by
1 post views Thread by jrodri1080 | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.