Array question

question about an array

Page 1 of 1

13 Replies - 1207 Views - Last Post: 26 February 2008 - 01:10 AM Rate Topic: -----

#1 rustler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-February 08

Array question

Posted 25 February 2008 - 09:55 PM

Hello my teacher is having me create an array and then store the class flight node in there. From flight node it stores the flight number for that node and also has a pointer to a queue for all people in standby for that flight. Right now I'm trying to make a way to store the flight node class in the array but am having trouble.
Using this code i get an error that says "The method FlightNode(int) is undefined for the type Array"
import javax.swing.JOptionPane;
public class Array {
	int x;
	String temp;
	public void init(int x)
	{
		FlightNode[] flight = new FlightNode[x];
		for(int i = 0; i < flight.length; i++)
		{
			temp = JOptionPane.showInputDialog("Please input a flight number.");
			flight[i] = FlightNode(Integer.parseInt(temp));
		}
	}
}


Here is my flightnode class
public class FlightNode {
	 public int data2;
		public int data1; // type of value that can be stored in a Node object
		public FlightNode previous; // points to the first node in the queue
		public FlightNode next;	// a reference to a Node object
		public StandbyList person; // Creates a new queue for the people to be added to. 

		/**
		 * Constructor for objects of class Node
		 */
		//creates two dummy nodes that make the rest adding other nodes easier.  
		
		public FlightNode(int dataItem)
		{
			data1 = dataItem;
			next = null;
			previous = null; 
			person = new StandbyList();
		}

		public FlightNode(int dataItem, FlightNode nodeReference)
		{
			data1 = dataItem;
			next = nodeReference;
		}
		
		public int get()
		{
			return (data1);
		}
	
}


Thanks in advance for your help

Is This A Good Question/Topic? 0
  • +

Replies To: Array question

#2 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Array question

Posted 25 February 2008 - 10:03 PM

I didn't check for any other errors, but you are missing the "new" keyword.

import javax.swing.JOptionPane;
public class Array {
    int x;
    String temp;
    public void init(int x)
    {
        FlightNode[] flight = new FlightNode[x];
        for(int i = 0; i < flight.length; i++)
        {
            temp = JOptionPane.showInputDialog("Please input a flight number.");
            flight[i] = new FlightNode(Integer.parseInt(temp));       //HERE, added "new"
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 rustler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-February 08

Re: Array question

Posted 25 February 2008 - 10:09 PM

Thanks a lot. Its compiling now
Was This Post Helpful? 0
  • +
  • -

#4 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Array question

Posted 25 February 2008 - 10:10 PM

No problem.
Was This Post Helpful? 0
  • +
  • -

#5 rustler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-February 08

Re: Array question

Posted 25 February 2008 - 10:39 PM

also i have a question now about printing off the flight number after they are entered in to the array. I'm think I need to do a for loop, and maybe use a get method from the FlightNode class to get the data value (which will be the flight number) for that flightnode in the array. However this is about as far as ive gotten so far. I'm assuming that even though the flight array is still known to the printflight since it is in the same class.
Is this thinking so far correct?
Was This Post Helpful? 0
  • +
  • -

#6 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Array question

Posted 25 February 2008 - 10:49 PM

You are correct. You can loop through your "flight" array in the init function and use the "get" method to get the flight numbers back.

Kinda confusing wording. Did you just want confirmation?
Was This Post Helpful? 0
  • +
  • -

#7 rustler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-February 08

Re: Array question

Posted 25 February 2008 - 11:54 PM

I am going to do it in a different method, becuase I'm gonna have to call it at speerate occasions. I wanted confirmation because right now i can't figure out the code to get it to work. When i used the get it said you can't get a static to non static method. So that confused me.
public void printFlights()
	{
		for(int i = 0; i < flight.length; i++)
		{
			System.out.println(FlightNode.get());
		}
	} 


This is what I have so far but it won't compile and I don't think even after i fix this problem it will work. Sorry about the confusing wording, as I'm not sure exactly how to ask the question.
Was This Post Helpful? 0
  • +
  • -

#8 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Array question

Posted 26 February 2008 - 12:00 AM

Okay. The way you have it right now won't work. Your for loop is calling get on a static FlightNode, which is not possible because obviously you want to call it on a specific node.

Assuming that your printFlights method is inside the "Array" class, you should make the "flight" array global. You should probably just swap the declarations of String temp and FlightNode[] flight. "temp" doesn't need to be global.
Doing that will allow you to make your loop just like you said before, by using flight[i].

Hope that helps.

This post has been edited by pertheusual: 26 February 2008 - 12:02 AM

Was This Post Helpful? 0
  • +
  • -

#9 rustler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-February 08

Re: Array question

Posted 26 February 2008 - 12:35 AM

I moved declared flight outside of the init method, but can't declare the length until inside the method since it depends on user input. So far here is my code below, but i fear i'm losing the array flight and its values after i leave init.
import javax.swing.JOptionPane;
public class Array {
	int x, y;
	String temp;
	FlightNode[] flight; 
	public void init(int x)
	{
		flight = new FlightNode[x];
		for(int i = 0; i < flight.length; i++)
		{
			temp = JOptionPane.showInputDialog("Please input a flight number.");
			flight[i] = new FlightNode(Integer.parseInt(temp));
		}
		
	}
	public void printFlights()
	{
		for(int i = 0; i < flight.length; i++)
		{
			System.out.println(flight[i]);
		}
	} 
}


When i run it, it print outs the memory locations of each flight node (for this case i made the array 3 long)
FlightNode@d3c6a3
FlightNode@1961581
FlightNode@5ddb6e
Was This Post Helpful? 0
  • +
  • -

#10 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Array question

Posted 26 February 2008 - 12:46 AM

Well, you need to have the get() in there to actually get the data, otherwise it will print memory addresses.

Per
Was This Post Helpful? 0
  • +
  • -

#11 rustler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-February 08

Re: Array question

Posted 26 February 2008 - 12:55 AM

Ok, but i'm confused as to where to put the get method in the print statement.
Was This Post Helpful? 0
  • +
  • -

#12 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Array question

Posted 26 February 2008 - 12:59 AM

When calling methods, they must be called on the specific object that you want the data from, so it would be like this:
System.out.println(flight[i].get());



Per
Was This Post Helpful? 0
  • +
  • -

#13 rustler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-February 08

Re: Array question

Posted 26 February 2008 - 01:05 AM

Awesome i have a success finally. Thank you so much for your help throughout this night, i never would have been able to get it done.
Was This Post Helpful? 0
  • +
  • -

#14 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Array question

Posted 26 February 2008 - 01:10 AM

No problem. Have a good night.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1