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

11-05-2007, 09:20 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
|
Help passing arraylist to another class
I'm writing a program that asks the user to input information regarding airline flights. The data is being collected by arraylists. I have created a class called Flight which I want to pass the completed arraylists to. Another class called Search is than going to based on the users request use the flight class to find and retrieve the matching flight data. I'm having trouble passing the arraylists to the Flight class which is holding up the program. I receive and error that states cannot find symbol; symbol: constructor Flight; location: class Flight. The constructor is in the class so not sure why it cannot find it. Here is my 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 = new ArrayList<String>();
ArrayList<String> flightNumArray = new ArrayList<String>();
ArrayList<String> originArray = new ArrayList<String>();
ArrayList<String> destinationArray = new ArrayList<String>();
int totalFlights = 0;
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();
totalFlights++;
nameInputField.setText("");
flightNumField.setText("");
originField.setText("");
destinationField.setText("");
airlineArray.add(airline);
flightNumArray.add(flightNumber);
originArray.add(origin);
destinationArray.add(destination);
JOptionPane.showMessageDialog(null, "Flight data added.\n" +
"Number of Flights Added: " + totalFlights);
}
if(ae.getSource() == clearButton){
nameInputField.setText("");
flightNumField.setText("");
originField.setText("");
destinationField.setText("");
}
if(ae.getSource() == searchButton){
Flight Flight = new Flight(airlineArray, flightNumArray, originArray, destinationArray);
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
}
import java.util.ArrayList;
class Flight {
int totalFlights = 0;
String airlineName;
String flightNumber;
String originCity;
String destinationCity;
public Flight(String airlineArray, String flightNumArray, String originArray, String destinationArray){
airlineName = getAirlineName();
flightNumber = getFlightNumber();
originCity = getOriginCity();
destinationCity = getDestinationCity();
totalFlights++;
}// end Flight constructor
public String getAirlineName(){
return airlineName;
}
public void setAirlineName(String arrayAN){
airlineName = arrayAN;
}
public String getFlightNumber(){
return flightNumber;
}
public void setFlightNumber(String arrayFN){
flightNumber = arrayFN;
}
public String getOriginCity(){
return originCity;
}
public void setOriginCity(String arrayOC){
originCity = arrayOC;
}
public String getDestinationCity(){
return destinationCity;
}
public void setDestinationCity(String arrayDC){
destinationCity = arrayDC;
}
public String toStringFlightInfo(){
return String.format("%d", airlineName);
}
} // end Flight class
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Search implements ActionListener{
JFrame s;
JButton flightNumSearchButton;
JButton citySearchButton;
JButton exitSearchButton;
JTextField searchFlightNum;
JTextField searchDestination;
public void searchApp(){
JLabel searchTitle = new JLabel("Flight Search");
searchTitle.setFont(new Font("Serif", Font.BOLD, 30));
JPanel searchTitlePanel = new JPanel();
searchTitlePanel.setPreferredSize(new Dimension(425, 50));
searchTitlePanel.add(searchTitle);
searchFlightNum = new JTextField(10);
JLabel searchNumber = new JLabel("Enter Flight Number to Search For: ");
searchNumber.setFont(new Font("Serif", Font.PLAIN, 14));
flightNumSearchButton = new JButton("Search");
flightNumSearchButton.setFont(new Font("Serif", Font.PLAIN, 14));
JPanel sfn = new JPanel();
sfn.setLayout(new BoxLayout(sfn, BoxLayout.X_AXIS));
sfn.setPreferredSize(new Dimension(425, 20));
sfn.add(searchNumber);
sfn.add(searchFlightNum);
sfn.add(flightNumSearchButton);
JLabel searchCity = new JLabel("Enter City to Search For: ");
searchCity.setFont(new Font("Serif", Font.PLAIN, 14));
searchDestination = new JTextField(10);
citySearchButton = new JButton("Search");
citySearchButton.setFont(new Font("Serif", Font.PLAIN, 14));
JPanel sc = new JPanel();
sc.setLayout(new BoxLayout(sc, BoxLayout.X_AXIS));
sc.setPreferredSize(new Dimension(425, 20));
sc.add(searchCity);
sc.add(searchDestination);
sc.add(citySearchButton);
exitSearchButton = new JButton("Exit Search");
exitSearchButton.setFont(new Font("Serif", Font.PLAIN, 14));
JPanel es = new JPanel();
es.setAlignmentX(475);
es.setAlignmentY(10);
es.add(exitSearchButton);
JPanel searchMain = new JPanel();
searchMain.add(searchTitlePanel, BorderLayout.NORTH);
searchMain.add(sfn, BorderLayout.CENTER);
searchMain.add(sc, BorderLayout.CENTER);
searchMain.add(es, BorderLayout.CENTER);
s = new JFrame();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.getContentPane().add(searchMain);
s.setSize(550, 200); // sets size of JFrame
s.setLocation(200, 200); // sets location of JFrame
s.setVisible(true); // makes JFrame visible
flightNumSearchButton.addActionListener(this);
citySearchButton.addActionListener(this);
exitSearchButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == exitSearchButton){
s.dispose();
}
if(e.getSource() == flightNumSearchButton){
String flightNumberSearch = searchFlightNum.getText();
}
if(e.getSource() == citySearchButton){
String destinationCitySearch = searchDestination.getText();
}
}
}
|
|

11-05-2007, 10:45 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
public class FlightInterface implements ActionListener {
...
ArrayList<String> airlineArray = new ArrayList<String>();
ArrayList<String> flightNumArray = new ArrayList<String>();
ArrayList<String> originArray = new ArrayList<String>();
ArrayList<String> destinationArray = new ArrayList<String>();
...
// This next statement is using a constructor whose
// method signature looks like this:
//Flight(ArrayList<String> list1, ArrayList<String> list2,
// ArrayList<String> list3, ArrayList<String> list4)
// so the jvm looks for it in the Flight class. The only
// constructor available has a method signature like this:
//Flight(String s1, String s2, String s3, String s4)
Flight Flight = new Flight(airlineArray, flightNumArray,
originArray, destinationArray);
class Flight {
...
int totalFlights = 0;
String airlineName;
String flightNumber;
String originCity;
String destinationCity;
public Flight(String airlineArray, String flightNumArray,
String originArray, String destinationArray){
// The idea of passing arguments into a constructor is that
// you can/will use these arguments to initialize or help
// to initialize member variables.
// The member variable airlineName is null/not_initialized.
// So the next line will get a return value of null from
// the method call.
airlineName = getAirlineName();
flightNumber = getFlightNumber();
originCity = getOriginCity();
destinationCity = getDestinationCity();
totalFlights++;
// To initialize the member variables (in class scope) with
// the local variables (the agruments) you would start with
//this.airlineName = airlineArray;
// The argument "airlineArray" is declared as a String.
// We would expect a String array to be declared as "String[]".
}//
|
|

11-06-2007, 06:31 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
|
Thanks Hardwired for the help
|
|

11-06-2007, 10:02 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 7
|
|
|
may God bless u all
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|