Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-03-2007, 11:48 PM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
Add data to an array
I have a program that I want to set up to allow the user to be able to input as much data at a time as they wish. The user puts the data into a jtextfield on a form and than hits a get data button to grab the information. Once it is grabbed I want to add the info to an array. I know how to add the data to an array within a for loop but when I tried to do it all the values in the array were the same no matter what I put into the form. Do I have to use a for loop to add the data to the array or can it be done with just statements. I ask because the array really is not a set size as the user can change the number of entries they enter each time.

Any thoughts?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-03-2007, 11:59 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Let us see your code where you filled the array. If your array is small, then you can do that without loop but if the array is large then you will need a loop.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-04-2007, 12:27 AM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
The array can vary as it depends on the number of flights to be added. Also I do have a class that is meant to process the data so not sure if it would be better once I grab I data to send it to that class and add to the array there. I will post that code at the end. Here is the source for the data being added to the loop

Code:
public void actionPerformed(ActionEvent ae){ if(ae.getSource() == getDataButton){ String airline = nameInputField.getText(); String flightNumber = flightNumField.getText(); String origin = originField.getText(); String destination = destinationField.getText(); nameInputField.setText(""); flightNumField.setText(""); originField.setText(""); destinationField.setText(""); for(int nameCounter = 0; nameCounter < name.length; nameCounter++){ name[nameCounter] = airline; } JOptionPane.showMessageDialog(null, "Flight data added."); }
And here is the codefor the whole thing:

Code:
import java.awt.event.*; import java.awt.*; import javax.swing.*; public class FlightInterface implements ActionListener { JButton exitButton; JButton getDataButton; JButton clearButton; JButton searchButton; JTextField nameInputField; JTextField flightNumField; JTextField originField; JTextField destinationField; String[] name = new String[1000]; private JPanel getContent() { JLabel title = new JLabel("Flight Info"); title.setFont(new Font("Serif", Font.BOLD, 30)); JPanel bannerPanel = new JPanel(); bannerPanel.setPreferredSize(new Dimension(300, 50)); bannerPanel.add(title); nameInputField = new JTextField(10); JLabel inputAirlineName = new JLabel("Airline Name: "); inputAirlineName.setFont(new Font("Serif", Font.PLAIN, 14)); JPanel namePanel = new JPanel(); namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.X_AXIS)); namePanel.setPreferredSize(new Dimension(200, 20)); namePanel.add(inputAirlineName); namePanel.add(nameInputField); JLabel inputFlightNum = new JLabel("Flight Number: "); inputFlightNum.setFont(new Font("Serif", Font.PLAIN, 14)); flightNumField = new JTextField(10); JPanel flightNumPanel = new JPanel(); flightNumPanel.setLayout(new BoxLayout(flightNumPanel, BoxLayout.X_AXIS)); flightNumPanel.setPreferredSize(new Dimension(200, 20)); flightNumPanel.add(inputFlightNum); flightNumPanel.add(flightNumField); JLabel inputOrigin = new JLabel("City of Origin: "); inputOrigin.setFont(new Font("Serif", Font.PLAIN, 14)); originField = new JTextField(20); JPanel originPanel = new JPanel(); originPanel.setLayout(new BoxLayout(originPanel, BoxLayout.X_AXIS)); originPanel.setPreferredSize(new Dimension(200, 20)); originPanel.add(inputOrigin); originPanel.add(originField); JLabel inputDestination = new JLabel("Destination City: "); inputDestination.setFont(new Font("Serif", Font.PLAIN, 14)); destinationField = new JTextField(20); JPanel destinationPanel = new JPanel(); destinationPanel.setLayout(new BoxLayout(destinationPanel, BoxLayout.X_AXIS)); destinationPanel.setPreferredSize(new Dimension(200, 20)); destinationPanel.add(inputDestination); destinationPanel.add(destinationField); JPanel blankPanel = new JPanel(); blankPanel.setLayout(new BoxLayout(blankPanel, BoxLayout.X_AXIS)); blankPanel.setPreferredSize(new Dimension(200, 20)); getDataButton = new JButton("Get Data"); getDataButton.setFont(new Font("Serif", Font.PLAIN, 14)); exitButton = new JButton("Exit"); exitButton.setFont(new Font("Serif", Font.PLAIN, 14)); searchButton = new JButton("Search"); searchButton.setFont(new Font("Serif", Font.PLAIN, 14)); clearButton = new JButton("Clear"); clearButton.setFont(new Font("Serif", Font.PLAIN, 14)); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.add(getDataButton); buttonPanel.add(clearButton); buttonPanel.add(searchButton); buttonPanel.add(exitButton); getDataButton.addActionListener(this); clearButton.addActionListener(this); searchButton.addActionListener(this); exitButton.addActionListener(this); JPanel mainPanel = new JPanel(); System.out.println("JPanel default layout manager = " + mainPanel.getLayout().getClass().getName()); mainPanel.add(bannerPanel, BorderLayout.NORTH); mainPanel.add(namePanel, BorderLayout.CENTER); mainPanel.add(flightNumPanel, BorderLayout.CENTER); mainPanel.add(originPanel, BorderLayout.CENTER); mainPanel.add(destinationPanel, BorderLayout.CENTER); mainPanel.add(blankPanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.SOUTH); return mainPanel; } public void actionPerformed(ActionEvent ae){ if(ae.getSource() == getDataButton){ String airline = nameInputField.getText(); String flightNumber = flightNumField.getText(); String origin = originField.getText(); String destination = destinationField.getText(); nameInputField.setText(""); flightNumField.setText(""); originField.setText(""); destinationField.setText(""); for(int nameCounter = 0; nameCounter < name.length; nameCounter++){ name[nameCounter] = airline; } JOptionPane.showMessageDialog(null, "Flight data added."); } if(ae.getSource() == clearButton){ nameInputField.setText(""); flightNumField.setText(""); originField.setText(""); destinationField.setText(""); } if(ae.getSource() == searchButton){ } if(ae.getSource() == exitButton){ System.exit(0); } } public static void main(String[] args) { // Using the variable "app" will help avoid problems // with the static modifier in your class. FlightInterface app = new FlightInterface(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(app.getContent()); f.setSize(300, 265); // sets size of JFrame f.setLocation(200, 200); // sets location of JFrame f.setVisible(true); // makes JFrame visible } // end main }
Code:
public class Flight { private String airlineName; private int flightNumber; private String originCity; private String destinationCity; private static int totalFlights = 0; public Flight(){ airlineName = getAirlineName(); flightNumber = getFlightNumber(); originCity = getOriginCity(); destinationCity = getDestinationCity(); totalFlights++; }// end Flight constructor public String getAirlineName(){ return airlineName; } public void setAirlineName(String airline){ airlineName = airline; } public int getFlightNumber(){ return flightNumber; } public void setFlightNumber(int flightNum){ flightNumber = flightNum; } public String getOriginCity(){ return originCity; } public void setOriginCity(String origin){ originCity = origin; } public String getDestinationCity(){ return destinationCity; } public void setDestinationCity(String destination){ destinationCity = destination; } public String toStringFlightInfo(){ return String.format("%d", airlineName); } } // end Flight class
Thanks for the help

Last edited by adlb1300 : 11-04-2007 at 05:22 AM. Reason: added info
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-04-2007, 10:34 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Code:
String[] name = new String[1000]; String airline = nameInputField.getText(); for(int nameCounter = 0; nameCounter < name.length; nameCounter++){ name[nameCounter] = airline; }
As far as i see from the above code, you have an array with a predefined size and you are filling it with the same value obtained from a textfield. So it is normal to have your array elements to have the same values which is the entered text from the user.

What is the input you are getting from user and what do you want to put inside your array exactly?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-04-2007, 02:29 PM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
The user will input 4 values - name of airline, flight number, city of origin and destination city. So I will have 4 arrays in all. I also want to try and have the array grow as data is added as the user should not be limited in the number of items they can add.

Thanks
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-04-2007, 03:28 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
If the data structure needs to grow, then you should not use arrays because arrays are defined to have size. To make an array grow, you need to redefine it and copy previous data to the new one which is cpu consuming.

You can use Vector or ArrayList! Vectors/ArrayLists can grow when necessary automatically.. You will just need to call their add methods to add a new item.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-04-2007, 11:40 PM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
JavaBean,

Thanks for the tip. I've added in an ArrayList and added code so I can see the output after each entry asking for the size of the ArrayList and the contents of it. Each time I add data I keep getting a size of 1 and only the most recent element. It is the same even if I call to a particular index value. I also tried specifing the capacity of the array and still the same result. Any thoughts on what 'm doing wrong?

Code:
import java.awt.event.*; import java.awt.*; import javax.swing.*; import java.util.ArrayList; public class FlightInterface implements ActionListener { JButton exitButton; JButton getDataButton; JButton clearButton; JButton searchButton; JTextField destinationField; JTextField nameInputField; JTextField flightNumField; JTextField originField; ArrayList<String> airlineArray; ArrayList<String> flightNumArray; ArrayList<String> originArray; ArrayList<String> destinationArray; private JPanel getContent() { JLabel title = new JLabel("Flight Info"); title.setFont(new Font("Serif", Font.BOLD, 30)); JPanel bannerPanel = new JPanel(); bannerPanel.setPreferredSize(new Dimension(300, 50)); bannerPanel.add(title); nameInputField = new JTextField(10); JLabel inputAirlineName = new JLabel("Airline Name: "); inputAirlineName.setFont(new Font("Serif", Font.PLAIN, 14)); JPanel namePanel = new JPanel(); namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.X_AXIS)); namePanel.setPreferredSize(new Dimension(200, 20)); namePanel.add(inputAirlineName); namePanel.add(nameInputField); JLabel inputFlightNum = new JLabel("Flight Number: "); inputFlightNum.setFont(new Font("Serif", Font.PLAIN, 14)); flightNumField = new JTextField(10); JPanel flightNumPanel = new JPanel(); flightNumPanel.setLayout(new BoxLayout(flightNumPanel, BoxLayout.X_AXIS)); flightNumPanel.setPreferredSize(new Dimension(200, 20)); flightNumPanel.add(inputFlightNum); flightNumPanel.add(flightNumField); JLabel inputOrigin = new JLabel("City of Origin: "); inputOrigin.setFont(new Font("Serif", Font.PLAIN, 14)); originField = new JTextField(20); JPanel originPanel = new JPanel(); originPanel.setLayout(new BoxLayout(originPanel, BoxLayout.X_AXIS)); originPanel.setPreferredSize(new Dimension(200, 20)); originPanel.add(inputOrigin); originPanel.add(originField); JLabel inputDestination = new JLabel("Destination City: "); inputDestination.setFont(new Font("Serif", Font.PLAIN, 14)); destinationField = new JTextField(20); JPanel destinationPanel = new JPanel(); destinationPanel.setLayout(new BoxLayout(destinationPanel, BoxLayout.X_AXIS)); destinationPanel.setPreferredSize(new Dimension(200, 20)); destinationPanel.add(inputDestination); destinationPanel.add(destinationField); JPanel blankPanel = new JPanel(); blankPanel.setLayout(new BoxLayout(blankPanel, BoxLayout.X_AXIS)); blankPanel.setPreferredSize(new Dimension(200, 20)); getDataButton = new JButton("Get Data"); getDataButton.setFont(new Font("Serif", Font.PLAIN, 14)); exitButton = new JButton("Exit"); exitButton.setFont(new Font("Serif", Font.PLAIN, 14)); searchButton = new JButton("Search"); searchButton.setFont(new Font("Serif", Font.PLAIN, 14)); clearButton = new JButton("Clear"); clearButton.setFont(new Font("Serif", Font.PLAIN, 14)); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.add(getDataButton); buttonPanel.add(clearButton); buttonPanel.add(searchButton); buttonPanel.add(exitButton); getDataButton.addActionListener(this); clearButton.addActionListener(this); searchButton.addActionListener(this); exitButton.addActionListener(this); JPanel mainPanel = new JPanel(); System.out.println("JPanel default layout manager = " + mainPanel.getLayout().getClass().getName()); mainPanel.add(bannerPanel, BorderLayout.NORTH); mainPanel.add(namePanel, BorderLayout.CENTER); mainPanel.add(flightNumPanel, BorderLayout.CENTER); mainPanel.add(originPanel, BorderLayout.CENTER); mainPanel.add(destinationPanel, BorderLayout.CENTER); mainPanel.add(blankPanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.SOUTH); return mainPanel; } public void actionPerformed(ActionEvent ae){ if(ae.getSource() == getDataButton){ String airline = nameInputField.getText(); String flightNumber = flightNumField.getText(); String origin = originField.getText(); String destination = destinationField.getText(); nameInputField.setText(""); flightNumField.setText(""); originField.setText(""); destinationField.setText(""); airlineArray = new ArrayList<String>(); airlineArray.add(airline); flightNumArray = new ArrayList<String>(); flightNumArray.add(flightNumber); originArray = new ArrayList<String>(); originArray.add(origin); destinationArray = new ArrayList<String>(); destinationArray.add(destination); JOptionPane.showMessageDialog(null, "Flight data added."); } if(ae.getSource() == clearButton){ nameInputField.setText(""); flightNumField.setText(""); originField.setText(""); destinationField.setText(""); } if(ae.getSource() == searchButton){ Search sf = new Search(); sf.searchApp(); } if(ae.getSource() == exitButton){ System.exit(0); } } public static void main(String[] args) { // Using the variable "app" will help avoid problems // with the static modifier in your class. FlightInterface app = new FlightInterface(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(app.getContent()); f.setSize(300, 265); // sets size of JFrame f.setLocation(200, 200); // sets location of JFrame f.setVisible(true); // makes JFrame visible } // end main }
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-05-2007, 12:47 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Because every time you try to add new data you are also recreating arraylists! Create your arraylists only once somewhere!
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-05-2007, 03:01 AM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
Doh That was a stupid mistake. Thanks for the help
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array Help bluegreen7hi New To Java 2 03-28-2008 03:25 AM
can anyone help... 2d Array Mark1989 New To Java 2 03-12-2008 09:59 PM
Would appreciate your help with 2d Array.. cloudkicker New To Java 1 02-11-2008 03:34 PM
Data array to file Java Tip Java Tips 0 01-16-2008 11:42 AM
add data into an array mispeed New To Java 9 11-08-2007 04:53 AM


All times are GMT +3. The time now is 04:41 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org