Results 1 to 20 of 22
Thread: Unlimited Array Elements
- 06-30-2010, 05:53 AM #1
Member
- Join Date
- Jun 2010
- Location
- Ohio
- Posts
- 9
- Rep Power
- 0
Unlimited Array Elements
I have an array of people, lets say 20. But later I need to add 20 more to the array. right now my code is come like this.
String array[] = new String[20];
now this will only let me add 20 strings to that. I want to be able to add as many string to that array as possible. How can I accomplish this.
- 06-30-2010, 06:43 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
There is a Collection class that implements all sorts of structures that grow as you add more elements to it. See List, Map, Set etc. You'll find what you need there. Alternatively, make your own class that implements a growing array, or some other structure, like a Linked List. That would be better practice in the long run.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 06-30-2010, 06:43 PM #3
Member
- Join Date
- Jun 2010
- Location
- Ohio
- Posts
- 9
- Rep Power
- 0
okay I was having trouble setting my arraylist with objects from a different class. Lets say I have a class name Add, where I set the names and the phone number of that person. but when i try to put this is an arraylist it doesnt let me. would i be able to do this?
- 06-30-2010, 06:51 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Think of types (classes) and instantiations (objects) thereof; think of a Person class that has a 'name' member and a 'phone' member and you can add Person objects to your Lists instead of just Strings and ints that have to represent those names and phone numbers? b.t.w an int is a very bad type for the representation of a phone number. You are working with Java so why do things the Fortran or Pascal old fashioned way?
kind regards,
Jos
- 06-30-2010, 06:56 PM #5
Member
- Join Date
- Jun 2010
- Location
- Ohio
- Posts
- 9
- Rep Power
- 0
Is there a way you can provide me a quick example?
- 06-30-2010, 08:05 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 06-30-2010, 10:21 PM #7
When you get errors, copy and paste them here with your questions.it doesnt let me
- 07-01-2010, 09:11 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Also copy your code here so we can see what it's doing...otherwise we're just guessing.
- 07-12-2010, 09:22 PM #9
Member
- Join Date
- Jun 2010
- Location
- Ohio
- Posts
- 9
- Rep Power
- 0
sorry it took so long
the IDE tells me, "Array required, but java.util.List<Add> found" not sure what this means. I am guessing that I just cant swap out my old array with and Arraylist.
Sorry about the problems I am rather new to java and programming in general.
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;
public class Addy extends javax.swing.JFrame {
int counter, index;
int max = 5;
List<Add> donorArray = new ArrayList<Add>();
//Add donorArray[] = new Add[max];
Add donor = new Add(); //create new donor object to be used
private Formatter file;
public void openFile(){
String dir = "C:\\data\\inventory.dat";
String dirs = "C:\\data";
try{
file = new Formatter(dir);
}
catch(Exception e){
try{
// Create directories
new File(dirs).mkdirs();
file = new Formatter(dir);
}catch (Exception f){//Catch exception if any
JOptionPane.showMessageDialog(null, f.getMessage());
}
}
}
public void addRecords(){
for(int i=0; i<=donorArray.length; i++){
file.format("%s", "Name: " + donorArray[i].getName() + "\n");
file.format("%s", "Adress: " + donorArray[i].getAddress() + "\n");
file.format("%s", "City: " + donorArray[i].getCity() + "\n");
file.format("%s", "State: " + donorArray[i].getState() + "\n");
file.format("%s", "Zip: " + donorArray[i].getZip() + "\n");
file.format("%s", "Phone: " + donorArray[i].getPhone() + "\n");
file.format("%s", "Number of Donations: " + donorArray[i].getDonations() + "\n");
file.format("%s", "Amount of Donations: " + donorArray[i].getAmount() + "\n");
file.format("%s", "Total: " + donorArray[i].getTotal() + "\n\n");
}
}
public void closeFile(){
file.close();
}
public Addy() {
//calls upon methods in class
initComponents();
setButtonActions();
draw();
}
private void draw(){
//draw frame and object
JFrame f = new JFrame("Draw Something");
f.setAlwaysOnTop(true);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );
Logo l = new Logo();
f.add(l);
f.setSize(225,80);
f.setVisible(true);
}
public void addPersons(){
//for(; counter <=5; counter++){ //Simple start of counter to check condition of entries
List<Add> donorA = new ArrayList<Add>();
//Add donorA = new Add(); //create new donor object to be used
//get donor object after user inputs data
donorA.setName(txtName.getText());
donorA.setAddress(txtAddy.getText());
donorA.setCity(txtCity.getText());
donorA.setState(txtState.getText());
donorA.setZip(txtZip.getText());
donorA.setPhone(txtPhone.getText());
do{
if(donorA.getDonations() < 0){ //Checks to see if condition is less than '0'
JOptionPane.showMessageDialog(null, "Please enter a positive number!");
return;
}
donorA.setDonations(Double.parseDouble(txtDonation s.getText()));
}while(donorA.getDonations() < 0); //Repeats statement if condition is not met
do{
if(donorA.getAmount() < 0){ //Checks to see if condition is less than '0'
JOptionPane.showMessageDialog(null, "Please enter a positive number!");
return;
}
donorA.setAmount(Double.parseDouble(txtAmount.getT ext()));
}while(donorA.getAmount() < 0); //Repeats statement if condition is not met
donorArray[counter] = donorA; //creates array values
counter++;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnFirst = new javax.swing.JButton();
btnPrev = new javax.swing.JButton();
btnNext = new javax.swing.JButton();
lblName = new javax.swing.JLabel();
lblAddy = new javax.swing.JLabel();
lblDonations = new javax.swing.JLabel();
lblAmount = new javax.swing.JLabel();
lblPhone = new javax.swing.JLabel();
txtName = new javax.swing.JTextField();
txtAddy = new javax.swing.JTextField();
txtPhone = new javax.swing.JTextField();
txtDonations = new javax.swing.JTextField();
txtTotal = new javax.swing.JTextField();
txtExit = new javax.swing.JButton();
lblCity = new javax.swing.JLabel();
lblState = new javax.swing.JLabel();
lblZip = new javax.swing.JLabel();
txtCity = new javax.swing.JTextField();
txtState = new javax.swing.JTextField();
txtZip = new javax.swing.JTextField();
btnLast = new javax.swing.JButton();
btnAdd = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
btnEdit = new javax.swing.JButton();
btnSearch = new javax.swing.JButton();
lblStat = new javax.swing.JLabel();
editSave = new javax.swing.JButton();
editCancel = new javax.swing.JButton();
saveSave = new javax.swing.JButton();
saveCancel = new javax.swing.JButton();
txtAmount = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
saveBook = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
btnFirst.setText("First Person");
btnFirst.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnFirstActionPerformed(evt);
}
});
btnPrev.setText("<< Previous");
btnPrev.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPrevActionPerformed(evt);
}
});
btnNext.setText("Next>>");
btnNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNextActionPerformed(evt);
}
});
lblName.setLabelFor(txtName);
lblName.setText("Name:");
lblAddy.setLabelFor(txtAddy);
lblAddy.setText("Address:");
lblDonations.setLabelFor(txtDonations);
lblDonations.setText("Number of Donations:");
lblAmount.setLabelFor(txtTotal);
lblAmount.setText("Total Amount Donated:");
lblPhone.setLabelFor(txtPhone);
lblPhone.setText("Phone:");
txtName.setEditable(false);
txtAddy.setEditable(false);
txtPhone.setEditable(false);
txtDonations.setEditable(false);
txtDonations.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtDonationsActionPerformed(evt);
}
});
txtTotal.setEditable(false);
txtTotal.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
txtTotal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtTotalActionPerformed(evt);
}
});
txtExit.setText("Exit");
txtExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtExitActionPerformed(evt);
}
});
lblCity.setLabelFor(txtCity);
lblCity.setText("City:");
lblState.setLabelFor(txtState);
lblState.setText("State:");
lblZip.setLabelFor(txtZip);
lblZip.setText("Zip:");
txtCity.setEditable(false);
txtState.setEditable(false);
txtZip.setEditable(false);
btnLast.setText("Last");
btnLast.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLastActionPerformed(evt);
}
});
btnAdd.setText("Add");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
btnDelete.setText("Delete");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});
btnEdit.setText("Edit");
btnEdit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEditActionPerformed(evt);
}
});
btnSearch.setText("Search");
btnSearch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSearchActionPerformed(evt);
}
});
lblStat.setFont(new java.awt.Font("Lucida Grande", 1, 13));
lblStat.setHorizontalAlignment(javax.swing.SwingCo nstants.CENTER);
lblStat.setText("Address Book");
editSave.setText("Save");
editSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
editSaveActionPerformed(evt);
}
});
editCancel.setText("Cancel");
editCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
editCancelActionPerformed(evt);
}
});
saveSave.setText("Save");
saveSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveSaveActionPerformed(evt);
}
});
saveCancel.setText("Cancel");
saveCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveCancelActionPerformed(evt);
}
});
txtAmount.setEditable(false);
jLabel1.setText("Amout of Each Donation:");
saveBook.setText("Save Book");
saveBook.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveBookActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADIN G, layout.createSequentialGroup()
.addContainerGap()
.addComponent(lblAmount)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(txtTotal))
.addGroup(javax.swing.GroupLayout.Alignment.LEADIN G, layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(btnSearch)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(btnAdd)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(btnDelete)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(btnEdit)
.addGap(104, 104, 104)
.addComponent(txtExit))
.addGroup(javax.swing.GroupLayout.Alignment.LEADIN G, layout.createSequentialGroup()
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
.addComponent(lblName)
.addComponent(lblAddy)
.addComponent(lblCity)
.addComponent(lblState)
.addComponent(lblZip))
.addGap(13, 13, 13))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
.addComponent(lblDonations)
.addComponent(lblPhone)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(lblStat, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)))
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
.addComponent(txtDonations)
.addComponent(txtPhone)
.addComponent(txtZip)
.addComponent(txtState)
.addComponent(txtCity)
.addComponent(txtName)
.addComponent(txtAddy, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.addComponent(txtAmount))
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(saveSave)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(saveCancel)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(editSave)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(editCancel)))))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(saveBook))
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(btnFirst)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(btnPrev)
.addGap(61, 61, 61)
.addComponent(btnNext)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(btnLast)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(btnFirst)
.addComponent(btnPrev)
.addComponent(btnLast)
.addComponent(btnNext))
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(lblStat, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
.addComponent(saveSave)
.addComponent(saveCancel)
.addComponent(editCancel)
.addComponent(editSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblName))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(txtAddy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblAddy))
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(lblCity)
.addComponent(txtCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(lblState)
.addComponent(txtState, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(lblZip)
.addComponent(txtZip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblPhone)))
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(saveBook)))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(txtDonations, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblDonations))
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(6, 6, 6)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblAmount))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(btnEdit)
.addComponent(btnDelete)
.addComponent(btnAdd)
.addComponent(btnSearch))
.addComponent(txtExit))
.addContainerGap())
);
pack();
}// </editor-fold>
private void txtDonationsActionPerformed(java.awt.event.ActionE vent evt) {
// TODO add your handling code here:
}
private void txtExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {
///Next Button
int l = donorArray.length;
l--;
if(index >= l){//if the index is the last in the array it will start over at the the first person
index = 0;
int i = index;
index--;
txtName.setText(donorArray[i].getName());
txtAddy.setText(donorArray[i].getAddress());
txtPhone.setText(donorArray[i].getPhone());
txtDonations.setText(String.valueOf(donorArray[i].getDonations()));
txtAmount.setText(String.valueOf(donorArray[i].getAmount()));
txtCity.setText(donorArray[i].getCity());
txtState.setText(donorArray[i].getState());
txtZip.setText(donorArray[i].getZip());
txtTotal.setText(String.valueOf(donorArray[i].getTotal()));
}//if not it will just add one to the index and go to the next person
index++;
int i = index;
txtName.setText(donorArray[i].getName());
txtAddy.setText(donorArray[i].getAddress());
txtPhone.setText(donorArray[i].getPhone());
txtDonations.setText(String.valueOf(donorArray[i].getDonations()));
txtAmount.setText(String.valueOf(donorArray[i].getAmount()));
txtCity.setText(donorArray[i].getCity());
txtState.setText(donorArray[i].getState());
txtZip.setText(donorArray[i].getZip());
txtTotal.setText(String.valueOf(donorArray[i].getTotal()));
}
private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {
index = 0;
txtName.setText(donorArray[0].getName());
txtAddy.setText(donorArray[0].getAddress());
txtPhone.setText(donorArray[0].getPhone());
txtDonations.setText(String.valueOf(donorArray[0].getDonations()));
txtAmount.setText(String.valueOf(donorArray[0].getAmount()));
txtCity.setText(donorArray[0].getCity());
txtState.setText(donorArray[0].getState());
txtZip.setText(donorArray[0].getZip());
txtTotal.setText(String.valueOf(donorArray[0].getTotal()));
}
private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {
//Last Button, simple goes the last person in the array
int i = donorArray.length;
i--;
index = i;
txtName.setText(donorArray[i].getName());
txtAddy.setText(donorArray[i].getAddress());
txtPhone.setText(donorArray[i].getPhone());
txtDonations.setText(String.valueOf(donorArray[i].getDonations()));
txtAmount.setText(String.valueOf(donorArray[0].getAmount()));
txtCity.setText(donorArray[i].getCity());
txtState.setText(donorArray[i].getState());
txtZip.setText(donorArray[i].getZip());
txtTotal.setText(String.valueOf(donorArray[i].getTotal()));
txtAmount.setText(String.valueOf(donorArray[i].getAmount()));
}
private void btnPrevActionPerformed(java.awt.event.ActionEvent evt) {
//Previous Button
int l = donorArray.length;
l--;
if(index <= 0){//if at the first person goes the last person in the array
index = l;
int i = index;
index++;
txtName.setText(donorArray[i].getName());
txtAddy.setText(donorArray[i].getAddress());
txtPhone.setText(donorArray[i].getPhone());
txtDonations.setText(String.valueOf(donorArray[i].getDonations()));
txtAmount.setText(String.valueOf(donorArray[i].getAmount()));
txtCity.setText(donorArray[i].getCity());
txtState.setText(donorArray[i].getState());
txtZip.setText(donorArray[i].getZip());
txtTotal.setText(String.valueOf(donorArray[i].getTotal()));
}//if not then it will subtract and goto the previous person
index--;
int i = index;
txtName.setText(donorArray[i].getName());
txtAddy.setText(donorArray[i].getAddress());
txtPhone.setText(donorArray[i].getPhone());
txtDonations.setText(String.valueOf(donorArray[i].getDonations()));
txtAmount.setText(String.valueOf(donorArray[i].getAmount()));
txtCity.setText(donorArray[i].getCity());
txtState.setText(donorArray[i].getState());
txtZip.setText(donorArray[i].getZip());
txtTotal.setText(String.valueOf(donorArray[i].getTotal()));
}
private void setButtonActions(){
editSave.setVisible(false);
editCancel.setVisible(false);
saveSave.setVisible(false);
saveCancel.setVisible(false);
}
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
//addPersons();
saveSave.setVisible(true);
saveCancel.setVisible(true);
btnAdd.setEnabled(false);
btnDelete.setEnabled(false);
btnEdit.setEnabled(false);
btnSearch.setEnabled(false);
txtName.setEditable(true);
txtAddy.setEditable(true);
txtPhone.setEditable(true);
txtDonations.setEditable(true);
txtAmount.setEditable(true);
txtCity.setEditable(true);
txtState.setEditable(true);
txtZip.setEditable(true);
lblStat.setText("Enter new Contact");
editSave.setEnabled(true);
editCancel.setEnabled(true);
editCancel.setVisible(false);
txtName.setText("");
txtAddy.setText("");
txtPhone.setText("");
txtDonations.setText("");
txtTotal.setText("");
txtCity.setText("");
txtState.setText("");
txtZip.setText("");
txtAmount.setText("");
}
private void btnDeleteActionPerformed(java.awt.event.ActionEven t evt) {
int i = index;
txtName.setText(null);
txtAddy.setText(null);
txtPhone.setText(null);
txtDonations.setText(null);
txtAmount.setText(null);
txtCity.setText(null);
txtState.setText(null);
txtZip.setText(null);
txtTotal.setText(null);
donorArray[i].setName(null);
donorArray[i].setAddress(null);
donorArray[i].setCity(null);
donorArray[i].setState(null);
donorArray[i].setZip(null);
donorArray[i].setPhone(null);
donorArray[i].setDonations(0.0);
donorArray[i].setAmount(0.0);
}
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {
int i = index;
btnDelete.setEnabled(false);
btnAdd.setEnabled(false);
btnSearch.setEnabled(false);
btnEdit.setEnabled(false);
editSave.setVisible(true);
editCancel.setVisible(true);
txtName.setEditable(true);
txtAddy.setEditable(true);
txtPhone.setEditable(true);
txtDonations.setEditable(true);
txtAmount.setEditable(true);
txtCity.setEditable(true);
txtState.setEditable(true);
txtZip.setEditable(true);
lblStat.setText("Contact is now Editable");
editSave.setEnabled(true);
editCancel.setEnabled(true);
}
private void editCancelActionPerformed(java.awt.event.ActionEve nt evt) {
txtName.setEditable(false);
txtAddy.setEditable(false);
txtPhone.setEditable(false);
txtDonations.setEditable(false);
txtAmount.setEditable(false);
txtCity.setEditable(false);
txtState.setEditable(false);
txtZip.setEditable(false);
lblStat.setText("No changes made");
editSave.setEnabled(false);
editCancel.setEnabled(false);
txtName.setText("");
txtAddy.setText("");
txtPhone.setText("");
txtDonations.setText("");
txtTotal.setText("");
txtCity.setText("");
txtState.setText("");
txtZip.setText("");
txtAmount.setText("");
saveSave.setVisible(false);
saveCancel.setVisible(false);
editSave.setVisible(false);
editCancel.setVisible(false);
btnSearch.setEnabled(true);
btnAdd.setEnabled(true);
btnDelete.setEnabled(true);
btnEdit.setEnabled(true);
}
private void editSaveActionPerformed(java.awt.event.ActionEvent evt) {
int i = index;
donor.setName(txtName.getText());
donor.setAddress(txtAddy.getText());
donor.setCity(txtCity.getText());
donor.setState(txtState.getText());
donor.setZip(txtZip.getText());
donor.setPhone(txtPhone.getText());
donor.setDonations(Double.parseDouble(txtDonations .getText()));
donor.setAmount(Double.parseDouble(txtTotal.getTex t()));
txtName.setEditable(false);
txtAddy.setEditable(false);
txtPhone.setEditable(false);
txtDonations.setEditable(false);
txtAmount.setEditable(false);
txtCity.setEditable(false);
txtState.setEditable(false);
txtZip.setEditable(false);
lblStat.setText("Contact is not Editable");
editSave.setEnabled(false);
editCancel.setEnabled(false);
editSave.setVisible(false);
editCancel.setVisible(false);
btnSearch.setEnabled(true);
btnAdd.setEnabled(true);
btnDelete.setEnabled(true);
btnEdit.setEnabled(true);
lblStat.setText("Contact was Saved");
donorArray[i] = donor;
}
private void btnSearchActionPerformed(java.awt.event.ActionEven t evt) {
String title = JOptionPane.showInputDialog("Please Enter A Name:");
int i = 0;
boolean found = false;
for(;i<donorArray.length;i++) {
if (donorArray[i].getName().equals(title)) {
found = true;
txtName.setText(donorArray[i].getName());
txtAddy.setText(donorArray[i].getAddress());
txtPhone.setText(donorArray[i].getPhone());
txtDonations.setText(String.valueOf(donorArray[i].getDonations()));
txtAmount.setText(String.valueOf(donorArray[i].getAmount()));
txtCity.setText(donorArray[i].getCity());
txtState.setText(donorArray[i].getState());
txtZip.setText(donorArray[i].getZip());
txtTotal.setText(String.valueOf(donorArray[i].getTotal()));
}
}
if(found == false){
JOptionPane.showMessageDialog(null, "No Person Found!");
}
}
private void saveSaveActionPerformed(java.awt.event.ActionEvent evt) {
addPersons();
saveSave.setVisible(false);
saveCancel.setVisible(false);
btnDelete.setEnabled(true);
btnEdit.setEnabled(true);
btnSearch.setEnabled(true);
btnAdd.setEnabled(true);
txtName.setEditable(false);
txtAddy.setEditable(false);
txtPhone.setEditable(false);
txtDonations.setEditable(false);
txtAmount.setEditable(false);
txtCity.setEditable(false);
txtState.setEditable(false);
txtZip.setEditable(false);
lblStat.setText("New Contact Saved");
}
private void saveCancelActionPerformed(java.awt.event.ActionEve nt evt) {
txtName.setEditable(false);
txtAddy.setEditable(false);
txtPhone.setEditable(false);
txtDonations.setEditable(false);
txtTotal.setEditable(false);
txtCity.setEditable(false);
txtState.setEditable(false);
txtAmount.setEditable(false);
txtZip.setEditable(false);
lblStat.setText("Contact not saved");
editSave.setEnabled(false);
editCancel.setEnabled(false);
txtName.setText("");
txtAddy.setText("");
txtPhone.setText("");
txtDonations.setText("");
txtAmount.setText("");
txtCity.setText("");
txtState.setText("");
txtZip.setText("");
txtAmount.setText("");
saveSave.setVisible(false);
saveCancel.setVisible(false);
btnSearch.setEnabled(true);
btnAdd.setEnabled(true);
btnDelete.setEnabled(true);
btnEdit.setEnabled(true);
}
private void txtTotalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void saveBookActionPerformed(java.awt.event.ActionEvent evt) {
openFile();
addRecords();
closeFile();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Addy().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnEdit;
private javax.swing.JButton btnFirst;
private javax.swing.JButton btnLast;
private javax.swing.JButton btnNext;
private javax.swing.JButton btnPrev;
private javax.swing.JButton btnSearch;
private javax.swing.JButton editCancel;
private javax.swing.JButton editSave;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel lblAddy;
private javax.swing.JLabel lblAmount;
private javax.swing.JLabel lblCity;
private javax.swing.JLabel lblDonations;
private javax.swing.JLabel lblName;
private javax.swing.JLabel lblPhone;
private javax.swing.JLabel lblStat;
private javax.swing.JLabel lblState;
private javax.swing.JLabel lblZip;
private javax.swing.JButton saveBook;
private javax.swing.JButton saveCancel;
private javax.swing.JButton saveSave;
private javax.swing.JTextField txtAddy;
private javax.swing.JTextField txtAmount;
private javax.swing.JTextField txtCity;
private javax.swing.JTextField txtDonations;
private javax.swing.JButton txtExit;
private javax.swing.JTextField txtName;
private javax.swing.JTextField txtPhone;
private javax.swing.JTextField txtState;
private javax.swing.JTextField txtTotal;
private javax.swing.JTextField txtZip;
// End of variables declaration
}
- 07-12-2010, 09:37 PM #10
Where/what line does the IDE point to as having an error?
Please use code tags when posting code. Your unformatted code is hard to read.
- 07-12-2010, 09:43 PM #11
Member
- Join Date
- Jun 2010
- Location
- Ohio
- Posts
- 9
- Rep Power
- 0
sorry about that wasnt thinking, anyway the lines below throw and ide error:
Along with many other linesJava Code:file.format("%s", "Name: " + donorArray[i].getName() + "\n"); file.format("%s", "Adress: " + donorArray[i].getAddress() + "\n"); file.format("%s", "City: " + donorArray[i].getCity() + "\n"); file.format("%s", "State: " + donorArray[i].getState() + "\n"); file.format("%s", "Zip: " + donorArray[i].getZip() + "\n"); file.format("%s", "Phone: " + donorArray[i].getPhone() + "\n"); file.format("%s", "Number of Donations: " +
Java Code:/** import javax.swing.JFrame; import javax.swing.JOptionPane; import java.io.*; import java.util.*; public class Addy extends javax.swing.JFrame { int counter, index; int max = 5; List<Add> donorArray = new ArrayList<Add>(); //Add donorArray[] = new Add[max]; Add donor = new Add(); //create new donor object to be used private Formatter file; public void openFile(){ String dir = "C:\\data\\inventory.dat"; String dirs = "C:\\data"; try{ file = new Formatter(dir); } catch(Exception e){ try{ // Create directories new File(dirs).mkdirs(); file = new Formatter(dir); }catch (Exception f){//Catch exception if any JOptionPane.showMessageDialog(null, f.getMessage()); } } } public void addRecords(){ for(int i=0; i<=donorArray.length; i++){ file.format("%s", "Name: " + donorArray[i].getName() + "\n"); file.format("%s", "Adress: " + donorArray[i].getAddress() + "\n"); file.format("%s", "City: " + donorArray[i].getCity() + "\n"); file.format("%s", "State: " + donorArray[i].getState() + "\n"); file.format("%s", "Zip: " + donorArray[i].getZip() + "\n"); file.format("%s", "Phone: " + donorArray[i].getPhone() + "\n"); file.format("%s", "Number of Donations: " + donorArray[i].getDonations() + "\n"); file.format("%s", "Amount of Donations: " + donorArray[i].getAmount() + "\n"); file.format("%s", "Total: " + donorArray[i].getTotal() + "\n\n"); } } public void closeFile(){ file.close(); } public Addy() { //calls upon methods in class initComponents(); setButtonActions(); draw(); } private void draw(){ //draw frame and object JFrame f = new JFrame("Draw Something"); f.setAlwaysOnTop(true); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Logo l = new Logo(); f.add(l); f.setSize(225,80); f.setVisible(true); } public void addPersons(){ //for(; counter <=5; counter++){ //Simple start of counter to check condition of entries List<Add> donorA = new ArrayList<Add>(); //Add donorA = new Add(); //create new donor object to be used //get donor object after user inputs data donorA.setName(txtName.getText()); donorA.setAddress(txtAddy.getText()); donorA.setCity(txtCity.getText()); donorA.setState(txtState.getText()); donorA.setZip(txtZip.getText()); donorA.setPhone(txtPhone.getText()); do{ if(donorA.getDonations() < 0){ //Checks to see if condition is less than '0' JOptionPane.showMessageDialog(null, "Please enter a positive number!"); return; } donorA.setDonations(Double.parseDouble(txtDonations.getText())); }while(donorA.getDonations() < 0); //Repeats statement if condition is not met do{ if(donorA.getAmount() < 0){ //Checks to see if condition is less than '0' JOptionPane.showMessageDialog(null, "Please enter a positive number!"); return; } donorA.setAmount(Double.parseDouble(txtAmount.getText())); }while(donorA.getAmount() < 0); //Repeats statement if condition is not met donorArray[counter] = donorA; //creates array values counter++; } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { btnFirst = new javax.swing.JButton(); btnPrev = new javax.swing.JButton(); btnNext = new javax.swing.JButton(); lblName = new javax.swing.JLabel(); lblAddy = new javax.swing.JLabel(); lblDonations = new javax.swing.JLabel(); lblAmount = new javax.swing.JLabel(); lblPhone = new javax.swing.JLabel(); txtName = new javax.swing.JTextField(); txtAddy = new javax.swing.JTextField(); txtPhone = new javax.swing.JTextField(); txtDonations = new javax.swing.JTextField(); txtTotal = new javax.swing.JTextField(); txtExit = new javax.swing.JButton(); lblCity = new javax.swing.JLabel(); lblState = new javax.swing.JLabel(); lblZip = new javax.swing.JLabel(); txtCity = new javax.swing.JTextField(); txtState = new javax.swing.JTextField(); txtZip = new javax.swing.JTextField(); btnLast = new javax.swing.JButton(); btnAdd = new javax.swing.JButton(); btnDelete = new javax.swing.JButton(); btnEdit = new javax.swing.JButton(); btnSearch = new javax.swing.JButton(); lblStat = new javax.swing.JLabel(); editSave = new javax.swing.JButton(); editCancel = new javax.swing.JButton(); saveSave = new javax.swing.JButton(); saveCancel = new javax.swing.JButton(); txtAmount = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); saveBook = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); btnFirst.setText("First Person"); btnFirst.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnFirstActionPerformed(evt); } }); btnPrev.setText("<< Previous"); btnPrev.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnPrevActionPerformed(evt); } }); btnNext.setText("Next>>"); btnNext.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnNextActionPerformed(evt); } }); lblName.setLabelFor(txtName); lblName.setText("Name:"); lblAddy.setLabelFor(txtAddy); lblAddy.setText("Address:"); lblDonations.setLabelFor(txtDonations); lblDonations.setText("Number of Donations:"); lblAmount.setLabelFor(txtTotal); lblAmount.setText("Total Amount Donated:"); lblPhone.setLabelFor(txtPhone); lblPhone.setText("Phone:"); txtName.setEditable(false); txtAddy.setEditable(false); txtPhone.setEditable(false); txtDonations.setEditable(false); txtDonations.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtDonationsActionPerformed(evt); } }); txtTotal.setEditable(false); txtTotal.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); txtTotal.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtTotalActionPerformed(evt); } }); txtExit.setText("Exit"); txtExit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtExitActionPerformed(evt); } }); lblCity.setLabelFor(txtCity); lblCity.setText("City:"); lblState.setLabelFor(txtState); lblState.setText("State:"); lblZip.setLabelFor(txtZip); lblZip.setText("Zip:"); txtCity.setEditable(false); txtState.setEditable(false); txtZip.setEditable(false); btnLast.setText("Last"); btnLast.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnLastActionPerformed(evt); } }); btnAdd.setText("Add"); btnAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddActionPerformed(evt); } }); btnDelete.setText("Delete"); btnDelete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDeleteActionPerformed(evt); } }); btnEdit.setText("Edit"); btnEdit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEditActionPerformed(evt); } }); btnSearch.setText("Search"); btnSearch.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSearchActionPerformed(evt); } }); lblStat.setFont(new java.awt.Font("Lucida Grande", 1, 13)); lblStat.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); lblStat.setText("Address Book"); editSave.setText("Save"); editSave.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { editSaveActionPerformed(evt); } }); editCancel.setText("Cancel"); editCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { editCancelActionPerformed(evt); } }); saveSave.setText("Save"); saveSave.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saveSaveActionPerformed(evt); } }); saveCancel.setText("Cancel"); saveCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saveCancelActionPerformed(evt); } }); txtAmount.setEditable(false); jLabel1.setText("Amout of Each Donation:"); saveBook.setText("Save Book"); saveBook.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saveBookActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addContainerGap() .addComponent(lblAmount) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtTotal)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGap(37, 37, 37) .addComponent(btnSearch) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnAdd) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnDelete) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnEdit) .addGap(104, 104, 104) .addComponent(txtExit)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGap(12, 12, 12) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(lblName) .addComponent(lblAddy) .addComponent(lblCity) .addComponent(lblState) .addComponent(lblZip)) .addGap(13, 13, 13)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(lblDonations) .addComponent(lblPhone) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblStat, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtDonations) .addComponent(txtPhone) .addComponent(txtZip) .addComponent(txtState) .addComponent(txtCity) .addComponent(txtName) .addComponent(txtAddy, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE) .addComponent(txtAmount)) .addGroup(layout.createSequentialGroup() .addGap(40, 40, 40) .addComponent(saveSave) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(saveCancel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(editSave) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(editCancel))))) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(saveBook)) .addGroup(layout.createSequentialGroup() .addGap(30, 30, 30) .addComponent(btnFirst) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnPrev) .addGap(61, 61, 61) .addComponent(btnNext) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnLast))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnFirst) .addComponent(btnPrev) .addComponent(btnLast) .addComponent(btnNext)) .addGap(25, 25, 25) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblStat, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE) .addComponent(saveSave) .addComponent(saveCancel) .addComponent(editCancel) .addComponent(editSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblName)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtAddy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblAddy)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblCity) .addComponent(txtCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblState) .addComponent(txtState, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblZip) .addComponent(txtZip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblPhone))) .addGroup(layout.createSequentialGroup() .addGap(21, 21, 21) .addComponent(saveBook))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtDonations, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblDonations)) .addGap(7, 7, 7) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addGap(6, 6, 6) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblAmount)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnEdit) .addComponent(btnDelete) .addComponent(btnAdd) .addComponent(btnSearch)) .addComponent(txtExit)) .addContainerGap()) ); pack(); }// </editor-fold> private void txtDonationsActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void txtExitActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } private void btnNextActionPerformed(java.awt.event.ActionEvent evt) { ///Next Button int l = donorArray.length; l--; if(index >= l){//if the index is the last in the array it will start over at the the first person index = 0; int i = index; index--; txtName.setText(donorArray[i].getName()); txtAddy.setText(donorArray[i].getAddress()); txtPhone.setText(donorArray[i].getPhone()); txtDonations.setText(String.valueOf(donorArray[i].getDonations())); txtAmount.setText(String.valueOf(donorArray[i].getAmount())); txtCity.setText(donorArray[i].getCity()); txtState.setText(donorArray[i].getState()); txtZip.setText(donorArray[i].getZip()); txtTotal.setText(String.valueOf(donorArray[i].getTotal())); }//if not it will just add one to the index and go to the next person index++; int i = index; txtName.setText(donorArray[i].getName()); txtAddy.setText(donorArray[i].getAddress()); txtPhone.setText(donorArray[i].getPhone()); txtDonations.setText(String.valueOf(donorArray[i].getDonations())); txtAmount.setText(String.valueOf(donorArray[i].getAmount())); txtCity.setText(donorArray[i].getCity()); txtState.setText(donorArray[i].getState()); txtZip.setText(donorArray[i].getZip()); txtTotal.setText(String.valueOf(donorArray[i].getTotal())); } private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) { index = 0; txtName.setText(donorArray[0].getName()); txtAddy.setText(donorArray[0].getAddress()); txtPhone.setText(donorArray[0].getPhone()); txtDonations.setText(String.valueOf(donorArray[0].getDonations())); txtAmount.setText(String.valueOf(donorArray[0].getAmount())); txtCity.setText(donorArray[0].getCity()); txtState.setText(donorArray[0].getState()); txtZip.setText(donorArray[0].getZip()); txtTotal.setText(String.valueOf(donorArray[0].getTotal())); } private void btnLastActionPerformed(java.awt.event.ActionEvent evt) { //Last Button, simple goes the last person in the array int i = donorArray.length; i--; index = i; txtName.setText(donorArray[i].getName()); txtAddy.setText(donorArray[i].getAddress()); txtPhone.setText(donorArray[i].getPhone()); txtDonations.setText(String.valueOf(donorArray[i].getDonations())); txtAmount.setText(String.valueOf(donorArray[0].getAmount())); txtCity.setText(donorArray[i].getCity()); txtState.setText(donorArray[i].getState()); txtZip.setText(donorArray[i].getZip()); txtTotal.setText(String.valueOf(donorArray[i].getTotal())); txtAmount.setText(String.valueOf(donorArray[i].getAmount())); } private void btnPrevActionPerformed(java.awt.event.ActionEvent evt) { //Previous Button int l = donorArray.length; l--; if(index <= 0){//if at the first person goes the last person in the array index = l; int i = index; index++; txtName.setText(donorArray[i].getName()); txtAddy.setText(donorArray[i].getAddress()); txtPhone.setText(donorArray[i].getPhone()); txtDonations.setText(String.valueOf(donorArray[i].getDonations())); txtAmount.setText(String.valueOf(donorArray[i].getAmount())); txtCity.setText(donorArray[i].getCity()); txtState.setText(donorArray[i].getState()); txtZip.setText(donorArray[i].getZip()); txtTotal.setText(String.valueOf(donorArray[i].getTotal())); }//if not then it will subtract and goto the previous person index--; int i = index; txtName.setText(donorArray[i].getName()); txtAddy.setText(donorArray[i].getAddress()); txtPhone.setText(donorArray[i].getPhone()); txtDonations.setText(String.valueOf(donorArray[i].getDonations())); txtAmount.setText(String.valueOf(donorArray[i].getAmount())); txtCity.setText(donorArray[i].getCity()); txtState.setText(donorArray[i].getState()); txtZip.setText(donorArray[i].getZip()); txtTotal.setText(String.valueOf(donorArray[i].getTotal())); } private void setButtonActions(){ editSave.setVisible(false); editCancel.setVisible(false); saveSave.setVisible(false); saveCancel.setVisible(false); } private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { //addPersons(); saveSave.setVisible(true); saveCancel.setVisible(true); btnAdd.setEnabled(false); btnDelete.setEnabled(false); btnEdit.setEnabled(false); btnSearch.setEnabled(false); txtName.setEditable(true); txtAddy.setEditable(true); txtPhone.setEditable(true); txtDonations.setEditable(true); txtAmount.setEditable(true); txtCity.setEditable(true); txtState.setEditable(true); txtZip.setEditable(true); lblStat.setText("Enter new Contact"); editSave.setEnabled(true); editCancel.setEnabled(true); editCancel.setVisible(false); txtName.setText(""); txtAddy.setText(""); txtPhone.setText(""); txtDonations.setText(""); txtTotal.setText(""); txtCity.setText(""); txtState.setText(""); txtZip.setText(""); txtAmount.setText(""); } private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) { int i = index; txtName.setText(null); txtAddy.setText(null); txtPhone.setText(null); txtDonations.setText(null); txtAmount.setText(null); txtCity.setText(null); txtState.setText(null); txtZip.setText(null); txtTotal.setText(null); donorArray[i].setName(null); donorArray[i].setAddress(null); donorArray[i].setCity(null); donorArray[i].setState(null); donorArray[i].setZip(null); donorArray[i].setPhone(null); donorArray[i].setDonations(0.0); donorArray[i].setAmount(0.0); } private void btnEditActionPerformed(java.awt.event.ActionEvent evt) { int i = index; btnDelete.setEnabled(false); btnAdd.setEnabled(false); btnSearch.setEnabled(false); btnEdit.setEnabled(false); editSave.setVisible(true); editCancel.setVisible(true); txtName.setEditable(true); txtAddy.setEditable(true); txtPhone.setEditable(true); txtDonations.setEditable(true); txtAmount.setEditable(true); txtCity.setEditable(true); txtState.setEditable(true); txtZip.setEditable(true); lblStat.setText("Contact is now Editable"); editSave.setEnabled(true); editCancel.setEnabled(true); } private void editCancelActionPerformed(java.awt.event.ActionEvent evt) { txtName.setEditable(false); txtAddy.setEditable(false); txtPhone.setEditable(false); txtDonations.setEditable(false); txtAmount.setEditable(false); txtCity.setEditable(false); txtState.setEditable(false); txtZip.setEditable(false); lblStat.setText("No changes made"); editSave.setEnabled(false); editCancel.setEnabled(false); txtName.setText(""); txtAddy.setText(""); txtPhone.setText(""); txtDonations.setText(""); txtTotal.setText(""); txtCity.setText(""); txtState.setText(""); txtZip.setText(""); txtAmount.setText(""); saveSave.setVisible(false); saveCancel.setVisible(false); editSave.setVisible(false); editCancel.setVisible(false); btnSearch.setEnabled(true); btnAdd.setEnabled(true); btnDelete.setEnabled(true); btnEdit.setEnabled(true); } private void editSaveActionPerformed(java.awt.event.ActionEvent evt) { int i = index; donor.setName(txtName.getText()); donor.setAddress(txtAddy.getText()); donor.setCity(txtCity.getText()); donor.setState(txtState.getText()); donor.setZip(txtZip.getText()); donor.setPhone(txtPhone.getText()); donor.setDonations(Double.parseDouble(txtDonations.getText())); donor.setAmount(Double.parseDouble(txtTotal.getText())); txtName.setEditable(false); txtAddy.setEditable(false); txtPhone.setEditable(false); txtDonations.setEditable(false); txtAmount.setEditable(false); txtCity.setEditable(false); txtState.setEditable(false); txtZip.setEditable(false); lblStat.setText("Contact is not Editable"); editSave.setEnabled(false); editCancel.setEnabled(false); editSave.setVisible(false); editCancel.setVisible(false); btnSearch.setEnabled(true); btnAdd.setEnabled(true); btnDelete.setEnabled(true); btnEdit.setEnabled(true); lblStat.setText("Contact was Saved"); donorArray[i] = donor; } private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) { String title = JOptionPane.showInputDialog("Please Enter A Name:"); int i = 0; boolean found = false; for(;i<donorArray.length;i++) { if (donorArray[i].getName().equals(title)) { found = true; txtName.setText(donorArray[i].getName()); txtAddy.setText(donorArray[i].getAddress()); txtPhone.setText(donorArray[i].getPhone()); txtDonations.setText(String.valueOf(donorArray[i].getDonations())); txtAmount.setText(String.valueOf(donorArray[i].getAmount())); txtCity.setText(donorArray[i].getCity()); txtState.setText(donorArray[i].getState()); txtZip.setText(donorArray[i].getZip()); txtTotal.setText(String.valueOf(donorArray[i].getTotal())); } } if(found == false){ JOptionPane.showMessageDialog(null, "No Person Found!"); } } private void saveSaveActionPerformed(java.awt.event.ActionEvent evt) { addPersons(); saveSave.setVisible(false); saveCancel.setVisible(false); btnDelete.setEnabled(true); btnEdit.setEnabled(true); btnSearch.setEnabled(true); btnAdd.setEnabled(true); txtName.setEditable(false); txtAddy.setEditable(false); txtPhone.setEditable(false); txtDonations.setEditable(false); txtAmount.setEditable(false); txtCity.setEditable(false); txtState.setEditable(false); txtZip.setEditable(false); lblStat.setText("New Contact Saved"); } private void saveCancelActionPerformed(java.awt.event.ActionEvent evt) { txtName.setEditable(false); txtAddy.setEditable(false); txtPhone.setEditable(false); txtDonations.setEditable(false); txtTotal.setEditable(false); txtCity.setEditable(false); txtState.setEditable(false); txtAmount.setEditable(false); txtZip.setEditable(false); lblStat.setText("Contact not saved"); editSave.setEnabled(false); editCancel.setEnabled(false); txtName.setText(""); txtAddy.setText(""); txtPhone.setText(""); txtDonations.setText(""); txtAmount.setText(""); txtCity.setText(""); txtState.setText(""); txtZip.setText(""); txtAmount.setText(""); saveSave.setVisible(false); saveCancel.setVisible(false); btnSearch.setEnabled(true); btnAdd.setEnabled(true); btnDelete.setEnabled(true); btnEdit.setEnabled(true); } private void txtTotalActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void saveBookActionPerformed(java.awt.event.ActionEvent evt) { openFile(); addRecords(); closeFile(); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Addy().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnAdd; private javax.swing.JButton btnDelete; private javax.swing.JButton btnEdit; private javax.swing.JButton btnFirst; private javax.swing.JButton btnLast; private javax.swing.JButton btnNext; private javax.swing.JButton btnPrev; private javax.swing.JButton btnSearch; private javax.swing.JButton editCancel; private javax.swing.JButton editSave; private javax.swing.JLabel jLabel1; private javax.swing.JLabel lblAddy; private javax.swing.JLabel lblAmount; private javax.swing.JLabel lblCity; private javax.swing.JLabel lblDonations; private javax.swing.JLabel lblName; private javax.swing.JLabel lblPhone; private javax.swing.JLabel lblStat; private javax.swing.JLabel lblState; private javax.swing.JLabel lblZip; private javax.swing.JButton saveBook; private javax.swing.JButton saveCancel; private javax.swing.JButton saveSave; private javax.swing.JTextField txtAddy; private javax.swing.JTextField txtAmount; private javax.swing.JTextField txtCity; private javax.swing.JTextField txtDonations; private javax.swing.JButton txtExit; private javax.swing.JTextField txtName; private javax.swing.JTextField txtPhone; private javax.swing.JTextField txtState; private javax.swing.JTextField txtTotal; private javax.swing.JTextField txtZip; // End of variables declaration }
-
If you're going to use ArrayLists, you'll then want to read the tutorials on how they're used. For one, you don't use array indices [i] but rather the get(int index) method. The tutorial should explain all.
- 07-12-2010, 09:51 PM #13
That line is referring to donorArray as if it were an array. Is it an array or what type is it?donorArray[i].getName()
The IDE is telling you what it sees and doesn't understand.
- 07-12-2010, 09:57 PM #14
Member
- Join Date
- Jun 2010
- Location
- Ohio
- Posts
- 9
- Rep Power
- 0
Ok the data type are objects from the Add class.
-
He's talking about the type of the donorArray variable which is an ArrayList. Again, you don't use indices or indexes with ArrayList -- don't do donorArray[i]. Instead use the get method. Again, please read the tutorials as they will explain better than we can.
- 07-12-2010, 10:01 PM #16
Is donorArray an array? The [] following its use in the statements the IDE points to says it should be. Is it?
- 07-12-2010, 10:05 PM #17
Member
- Join Date
- Jun 2010
- Location
- Ohio
- Posts
- 9
- Rep Power
- 0
DonorArray was an array of type Add, which I converted to an arraylist to grow as I add more contacts.
- 07-12-2010, 10:17 PM #18
Now you have to change all usages of it to match its new type. Fubarable has explained what you need to do to use that type.DonorArray was an array
- 07-12-2010, 10:38 PM #19
Member
- Join Date
- Jun 2010
- Location
- Ohio
- Posts
- 9
- Rep Power
- 0
Kind of figured that out after reading that. I will read the tutorial, do you know the link for it.
- 07-13-2010, 08:54 AM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
The Collections tutorial.
ETA: Oh yes, and when I requested the code (and I admit I didn't word it terribly well) I meant the relevant bit of code for the error...;)
Similar Threads
-
How array elements gets default value
By Veangat in forum New To JavaReplies: 1Last Post: 03-07-2010, 01:29 PM -
Get value of a String into elements of an Array.
By mainy in forum New To JavaReplies: 1Last Post: 08-01-2009, 09:17 PM -
comparing elements in array
By garyscott101 in forum New To JavaReplies: 14Last Post: 12-10-2008, 03:01 PM -
reference to elements in array
By Igor in forum New To JavaReplies: 1Last Post: 12-14-2007, 11:56 AM -
Help with array of elements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks