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 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.
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 ObjectOutputStr eam
And if possible have a look after a Serializable
Enjoy your code.
Good Luck !
Debasis Jana
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
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 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...
|
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,
|
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...
|
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
|
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 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
|
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...
|
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
|
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
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |