Hey guys. I am creating an ordered list using an array. Not an ArrayList or anything else. I can not figure out how to get all of the data from the OrderedList constructor to carry through to my "insert" method. Therefore, when I am checking if "count == 0" it knows what count is. Currently, as the code sits, insert does not know any of the variables created in the constructor. I am a little rusty in Java right now and I am hoping it isn't something simple.

Lines 66-70 are commented out to see if lines 57-61 would allow the program to recognized the data it is testing for in the if statements below it. If anybody could help get a jump start to a solution I would greatly appreciate it.

/**
 * 
 * @author Harry
 * 
 */

class Vehicle 
{
	public Vehicle(int x) 
	{
		vin = x;
		d = this.getDescription();
	}
	
	private int vin;
	private static String d;
	private int count = 0;
	
	public int getVin()
	{
		return vin;
	}
	
	public String getDescription() 
	{
		return d;
	}
	
	public int getCount()
	{
		return count;
	}
	
	public static void setDescription(String describe) 
	{
		d = describe;
	}
	
	// main: driver for vehicle class\
//	public static void main(String[] argv) 
//	{	
//		Vehicle v = new Vehicle(1234567890);
//		System.out.println("VIN Number: " + v.getVin());
//		
//		setDescription("Silver four-door sports sedan");
//		System.out.println("Description: " + v.getDescription());
//	}	
}


class OrderedList
{	
	public OrderedList(Vehicle v)
	{
		int vin = v.getVin();
		int max = 5;
		Vehicle[] array;
		array = new Vehicle[max];
		int count = v.getCount();
	}
	
	// insert vehicle to array
	public void insert(Vehicle v)
	{
//		int vin = v.getVin();
//		int max = 5;
//		Vehicle[] array;
//		array = new Vehicle[max];
//		int count = v.getCount();
		
		// Array at max capacity
		if (count == max)
		{
			System.out.println("List of vehicles is full.");
			return;
		}
		
		// Array is empty
		if (count == 0)
		{
			// Add first vehicle to list
			array[0] = v;
			count++;
		}
		
		// Array has Vehicles in it
		if (count > 0)
		{
			// Add next entry in order
			for (int i=0; i<count; i++)
			{
				// Duplicate
				if (vin == array[i].getVin())
				{	System.out.println("Vehicle with same VIN is already in the list.");
					return;
				}
				
				// New VIN is numerically lower than corresponding
				// vehicle in the array.
				if (vin < array[i].getVin())
				{
					// Shift elements right, add Vehicle
					for (int j=0; j < count; j++)
					{
						array[j] = v;
						Vehicle tmp = array[j];
						array[j] = array[j+1];
						array[j] = tmp;
						count++;
					}	
				}
				
				// New VIN is numerically higher than corresponding
				// vehicle in the array.
				if (vin > array[i].getVin())
				{
					// Shift elements left, add Vehicle
					for (int k = 0; k < count; k--)
					{
						array[k] = v;
						Vehicle tmp = array[k];
						array[k] = array[k-1];
						array[k] = tmp;
						count++;
					}	
				}
			}
		}
	}
	
	// Remove Vehicle from list
	public void remove(int id)
	{
		int count = v.getCount();
		int vin = id;
		for (int i=0; i<count; i++)
		{
			// ALGORITHM TO REMOVE
			// VEHICLE GOES HERE...
		}
	}
	
	// Find Vehicle in list
	boolean find(int vin)
	{
		// ALGORITHM TO FIND
		// VEHICLE GOES HERE...
	}
	
	
}

what do you have in your main method?
with this I mean: how have you tried to call your insert method?
is this code all within one file, or separate files, ...?
can you be a bit more specific?

i see in your for statements you are trying to transverse the array using a set length 'count'? why? and where does count get its max value because you have no setCount() nor can i see assignment to count etc? also rather use:

// Add next entry in order
			for (int i=0; i<array.length(); i++)//variable array length
			{}

not only that but look at your for arrays where do you reset count for the next array etc? is this how your complete source looks so far, could you post the source you use for testing that gives you certain outputs so far or is this it?

what do you have in your main method?
with this I mean: how have you tried to call your insert method?
is this code all within one file, or separate files, ...?
can you be a bit more specific?

Sure. I have nothing in my main method because Eclipse is telling me there are errors when lines 56-60 are active and the code wont run. The code is all within one class called Vehicle.java

i see in your for statements you are trying to transverse the array using a set length 'count'? why? and where does count get its max value because you have no setCount() nor can i see assignment to count etc? also rather use:

// Add next entry in order
			for (int i=0; i<array.length(); i++)//variable array length
			{}

not only that but look at your for arrays where do you reset count for the next array etc? is this how your complete source looks so far, could you post the source you use for testing that gives you certain outputs so far or is this it?

I use "count" because I don't want to traverse the full length of the array when there are possibly empty spots in it. Since those spaces in mem. do not matter, there is no need to check their VIN numbers. My max value is set at line 57. Count was at line 58, but I moved it up to line 18 to try to fix the issue this whole thread is about (forgot to add setCount() when I did getCount(). I did initiate it though, in line 18. Then in the if statements it gets incremented (and later decremented when I write "remove(Vehicle vin)". This is all the source here. I only tested the Vehicle class. I don't reset count, I maintain count throughout the program (that is my goal).

Again, the reason I didn't test OrderedList is because the errors Eclipse was showing me when I activate lines 56-60 instead of the identical lines just below.

if you have nothing in your main method, than how do you expect to test what you've done?

if you have nothing in your main method, than how do you expect to test what you've done?

When I tried to create a main method in the class "Vehicle" to test class OrderedList, I got errors in the code. Why try running it when it is already showing me that there are errors?

because if you do, you can use the error messages to check what you are doing wrong.
and, if you don't test your code, how are you to tell when it works or not?

because if you do, you can use the error messages to check what you are doing wrong.
and, if you don't test your code, how are you to tell when it works or not?

You may know this, but in the Eclipse IDE you can tell whether the code will work or not before you run it, and also has suggestions of fixing it (although they are lots of times useless) or just plain tells you what the problem is. I'll get some error messages, but do you see any reason why the code won't run as it sits? Shouldn't the data in the constructor be used throughout the methods within this class?

not always. I doubt Eclipse will inform you of runtime errors before actually running.
also, it points you to exactly the line in your code where there's a problem, comes in quite handy when solving bugs.

not always. I doubt Eclipse will inform you of runtime errors before actually running.
also, it points you to exactly the line in your code where there's a problem, comes in quite handy when solving bugs.

Gotcha. Check my edited post. Thanks!

looks like it, but that's not the case in your inner class

if you take care of the scope of your variables of your inner class, add a return value to the method that should return a boolean, but where you don't return anything,

you can uncomment your main method and run it.

expansion of stultuske above post... well to answer does this code work as it stands.... no

this is what i found, your Vehicle class should be declared as public as its the main class with the main method,

public class Vehicle {}

next you should uncomment your main method. also your remove() method should accept Vehicle as an argument like so

public void remove(int id,Vehicle v){}

and also you must uncomment your count ect in the insert method,and lastly your find() method should be:

boolean find(int vin) {
		// ALGORITHM TO FIND
		// VEHICLE GOES HERE...
            boolean bool=true;
            return bool;
	}

Well to answer my original question as to why the data in the constructor of OrderedList is not recognized: I need to move that data outside my constructor like I had already done in the Vehicle class. I knew it was something so simple. My professor finally made it to her office and took 2 seconds to answer my question just by looking at it.

expansion of stultuske above post... well to answer does this code work as it stands.... no

this is what i found, your Vehicle class should be declared as public as its the main class with the main method,

public class Vehicle {}

next you should uncomment your main method. also your remove() method should accept Vehicle as an argument like so

public void remove(int id,Vehicle v){}

and also you must uncomment your count ect in the insert method,and lastly your find() method should be:

boolean find(int vin) {
		// ALGORITHM TO FIND
		// VEHICLE GOES HERE...
            boolean bool=true;
            return bool;
	}

Right, def won't run. My Vehicle class should be public? DUH! Thanks man, another simple fix. my public void remove can only take "int id" per instructions of the prof. As for the find method, thanks for the correction there.

Vehicle class may be public. only has to be public if you want to call it from outside that package, but since the main method is in that one, it's no error not to make it public.

my public void remove can only take "int id" per instructions of the prof.

Then your Vehicle v variable should be global.

Then your Vehicle v variable should be global.

There must be another way, no? She doesn't expect or want us to make anything global.

Here is all of my source, updated. Currently working on a new algorithm to shift the elements of the array in their required direction to make the new element go in the correct spot. I will be working from the back of the array as I think that will be easier.

/**
 * 
 * @author Harry 
 * 
 */

public class Vehicle 
{
	private int vin;
	private static String d;
	  // static String d because it will otherwise return "null"
	  // for vehicle's description when tested in main.
	
	public Vehicle(int x) 
	{
		vin = x;
		d = this.getDescription();
	}
	
	// Accessors and modifiers
	public int getVin()
	{
		return vin;
	}
	
	public String getDescription() 
	{
		return d;
	}
	
	public static void setDescription(String describe) 
	{
		d = describe;
	}
	
	// Main method
	public static void main(String[] argv) 
	{	
		// Testing Vehicle.java
		System.out.println("Testing the Vehicle class...");
		Vehicle v = new Vehicle(1234567890);
		System.out.println("  VIN Number: " + v.getVin());
		
		setDescription("Silver four-door sports sedan");
		System.out.println("  Description: " + v.getDescription() + "\n");
		
		// Testing OrderedList.java
		System.out.println("Testing the OrderedList class...");
		// . . . .
	}
}


class OrderedList
{	
	private int vin;
	int max = 5;
	int count = 0;
	Vehicle[] array = new Vehicle[max];
	
	public OrderedList(Vehicle v)
	{
		vin = v.getVin();
	}
	
	// Insert vehicle to array
	public void insert(Vehicle v)
	{
		// Verify that the new VIN is not already in the list with find()
		// If found, then leave, else insert it and work from the back
		// of the array.
		
		// Array at max capacity
		if (count == max)
		{
			System.out.println("List of vehicles is full.");
			return;
		}
		
		// Array is empty
		if (count == 0)
		{
			// Add first vehicle to list
			array[0] = v;
			count++;
		}
		
		// Array has Vehicles in it
		if (count > 0)
		{
			// Add next entry in order
			// Working from back of array
			for (int i=count-1; i<count; i--)
			{
				// Duplicate
				if (vin == array[i].getVin())
				{	System.out.println("Vehicle with same VIN is already in the list.");
					return;
				}
				
				// New VIN is numerically lower than corresponding
				// vehicle in the array.
				if (vin < array[i].getVin())
				{
					// Shift elements, add Vehicle
					// Note: Working from the back of the array
					for (int j=count-1; j<count; j--)
					{
	                                        // NEW ALG. TO FIND 
						// CORRECT PLACEMENT
						
						// Code below used for working
						// from the front of array, disregard
						Vehicle tmp = array[j];
						array[j] = array[j-1];
						array[j] = tmp;
						count++;
					}	
				}
				
				// New VIN is numerically higher than corresponding
				// vehicle in the array.
				if (vin > array[i].getVin())
				{
					// Shift elements, add Vehicle
					for (int k=count-1; k<count; k--)
					{
						// NEW ALG. TO FIND
						// CORRECT PLACEMENT
						
						// Again, below used for 
						// working from front of array
						array[k] = v;
						Vehicle tmp = array[k];
						array[k] = array[k+1];
						array[k] = tmp;
						count++;
					}	
				}
			}
		}
	}
	
	// Remove Vehicle from list
	public void remove(int id)
	{
		// ALGORITHM TO REMOVE
		// VEHICLE GOES HERE...
	}
	
	// Find Vehicle in list
	boolean find(int vin)
	{
		// ALGORITHM TO FIND
		// VEHICLE GOES HERE...
		
		boolean bool = true;
		return bool;
	}
}

I think you are wrong because you already have a few global variables declared:

public class Vehicle 
{
	private int vin;//global variable
	private static String d;
       //static Vehicle v=new Vehicle();
}

So as I added the 'v' for Vehicle that would be making it global, which is the only way you will parse 'v' between the 2 classes unless you make a getMethod() or something

Oh I see. I didn't realize that was making it global. Prof. didn't like the fact that I made "String d" static and said it's probably because I've done something wrong. I know she is right. I am going to start fresh with all of my source tonight and see what I come up with.

Oh I see. I didn't realize that was making it global. Prof. didn't like the fact that I made "String d" static and said it's probably because I've done something wrong. I know she is right. I am going to start fresh with all of my source tonight and see what I come up with.

yes because by making it static you made only one of it exist at a time where as there must be multiple descriptions for the different vehicles. good luck.

here's a good read on static identifier:http://www.codestyle.org/java/faq-Static.shtml

yes because by making it static you made only one of it exist at a time where as there must be multiple descriptions for the different vehicles. good luck.

here's a good read on static identifier:http://www.codestyle.org/java/faq-Static.shtml

Although I did understand what static meant (1 copy avail at all times), I just was not thinking about what I was doing. Just wanted the red error underline to go away so that I could test some code. I'm going to read up and learn some more on static identifiers, thanks.

If I post my yet again updated code, can somebody point me in the right direction on how to add something to my array in main? I have no idea how to do this, it has been a minute since I've done this.

/**
 * 
 * @author Harry
 * 
 */

public class Vehicle 
{
	private int vin;
	private static String d;
	  // static String d, else main will return "null"
	  // for vehicle's description when tested.
	
	public Vehicle(int x) 
	{
		vin = x;
		d = this.getDescription();
	}
	
	// Accessors and modifiers
	public int getVin()
	{
		return vin;
	}
	
	public String getDescription() 
	{
		return d;
	}
	
	public static void setDescription(String describe) 
	{
		d = describe;
	}
	
	// Main method
	public static void main(String[] argv) 
	{	
		// Testing Vehicle.java
		System.out.println("Testing the Vehicle class...");
		Vehicle v = new Vehicle(1234567890);
		System.out.println("  VIN Number: " + v.getVin());
		setDescription("Silver four-door sports sedan");
		System.out.println("  Description: " + v.getDescription() + "\n");
		
		// Testing OrderedList.java
		System.out.println("Testing the OrderedList class...");
		// . . . .
	}
}


class OrderedList
{	
	private int vin;
	int max = 5;
	int count = 0;
	Vehicle[] array = new Vehicle[max];
	
	public OrderedList(Vehicle v)
	{
		vin = v.getVin();
	}
	
	// Insert vehicle to array
	public void insert(Vehicle v)
	{
		
		// @TODO CODE
		// Verify that the new VIN is not already in the list with find()
		// If found, then leave, else insert v.
		
		// Array at max capacity
		if (count == max)
		{
			System.out.println("List of vehicles is full.");
			return;
		}
		
		// Array is empty
		if (count == 0)
		{
			// Add first vehicle to list
			array[0] = v;
			count++;
		}
		
		// Array has Vehicles in it
		if (count > 0)
		{
			// Add next entry in order
			// Working from back of array
			for (int i=count-1; i<count; i--)
			{
				// Duplicate
				if (vin == array[i].getVin())
				{	System.out.println("Vehicle with same VIN is already in the list.");
					return;
				}
				
				// New VIN is numerically lower than corresponding
				// vehicle in the array.
				if (vin < array[i].getVin())
				{
					// Shift elements to the right, add Vehicle
					for (int j=count-1; j<count; j--)
					{
						Vehicle tmp = v;
						array[j+1] = array[j];
						array[j] = tmp;
//						count++;
					}	
				}
				
				// New VIN is numerically higher than corresponding
				// vehicle in the array.
				if (vin > array[i].getVin())
				{
					// Add Vehicle in [i+1]
					for (int k=count-1; k<count; k--)
					{
						Vehicle tmp = v;
						array[i+1] = tmp;
					}	
				}
				count++;
			}
		}
	}
	
	// Remove Vehicle from list
	public void remove(int vin)
	{
			// ALGORITHM TO REMOVE VEHICLE
			// WITH CORRESPONDING vin. IF
			// NO SUCH VEHICLE, DO NOTHING
	}
	
	// Find Vehicle in list
	boolean find(int vin)
	{
		// ALGORITHM TO FIND VEHICLE IN
		// LIST WITH CORRESPONDING vin 
		// HERE. MUST USE BINARY SEARCH
		
		boolean bool = x;
		return bool;
	}
	
	public void retrieve(int vin)
	{
		// ALGORITHM TO RETURN A REFERENCE
		// TO THE VEHICLE WITH vin, RETURNS
		// NULL IF NO SUCH VEHICLE IN LIST
	}
	
	public void display()
	{
		// ALGORITHM TO DISPLAY ALL
		// ELEMENTS IN ARRAY HERE
	}
	
	
}

ehm ... which array, you have no array in your main method.

If I post my yet again updated code, can somebody point me in the right direction on how to add something to my array in main? I have no idea how to do this, it has been a minute since I've done this.

/**
 * 
 * @author Harry
 * 
 */

public class Vehicle 
{
	private int vin;
	private static String d;
	  // static String d, else main will return "null"
	  // for vehicle's description when tested.
	
	public Vehicle(int x) 
	{
		vin = x;
		d = this.getDescription();
	}
	
	// Accessors and modifiers
	public int getVin()
	{
		return vin;
	}
	
	public String getDescription() 
	{
		return d;
	}
	
	public static void setDescription(String describe) 
	{
		d = describe;
	}
	
	// Main method
	public static void main(String[] argv) 
	{	
		// Testing Vehicle.java
		System.out.println("Testing the Vehicle class...");
		Vehicle v = new Vehicle(1234567890);
		System.out.println("  VIN Number: " + v.getVin());
		setDescription("Silver four-door sports sedan");
		System.out.println("  Description: " + v.getDescription() + "\n");
		
		// Testing OrderedList.java
		System.out.println("Testing the OrderedList class...");
		// . . . .
	}
}


class OrderedList
{	
	private int vin;
	int max = 5;
	int count = 0;
	Vehicle[] array = new Vehicle[max];
	
	public OrderedList(Vehicle v)
	{
		vin = v.getVin();
	}
	
	// Insert vehicle to array
	public void insert(Vehicle v)
	{
		
		// @TODO CODE
		// Verify that the new VIN is not already in the list with find()
		// If found, then leave, else insert v.
		
		// Array at max capacity
		if (count == max)
		{
			System.out.println("List of vehicles is full.");
			return;
		}
		
		// Array is empty
		if (count == 0)
		{
			// Add first vehicle to list
			array[0] = v;
			count++;
		}
		
		// Array has Vehicles in it
		if (count > 0)
		{
			// Add next entry in order
			// Working from back of array
			for (int i=count-1; i<count; i--)
			{
				// Duplicate
				if (vin == array[i].getVin())
				{	System.out.println("Vehicle with same VIN is already in the list.");
					return;
				}
				
				// New VIN is numerically lower than corresponding
				// vehicle in the array.
				if (vin < array[i].getVin())
				{
					// Shift elements to the right, add Vehicle
					for (int j=count-1; j<count; j--)
					{
						Vehicle tmp = v;
						array[j+1] = array[j];
						array[j] = tmp;
//						count++;
					}	
				}
				
				// New VIN is numerically higher than corresponding
				// vehicle in the array.
				if (vin > array[i].getVin())
				{
					// Add Vehicle in [i+1]
					for (int k=count-1; k<count; k--)
					{
						Vehicle tmp = v;
						array[i+1] = tmp;
					}	
				}
				count++;
			}
		}
	}
	
	// Remove Vehicle from list
	public void remove(int vin)
	{
			// ALGORITHM TO REMOVE VEHICLE
			// WITH CORRESPONDING vin. IF
			// NO SUCH VEHICLE, DO NOTHING
	}
	
	// Find Vehicle in list
	boolean find(int vin)
	{
		// ALGORITHM TO FIND VEHICLE IN
		// LIST WITH CORRESPONDING vin 
		// HERE. MUST USE BINARY SEARCH
		
		boolean bool = x;
		return bool;
	}
	
	public void retrieve(int vin)
	{
		// ALGORITHM TO RETURN A REFERENCE
		// TO THE VEHICLE WITH vin, RETURNS
		// NULL IF NO SUCH VEHICLE IN LIST
	}
	
	public void display()
	{
		// ALGORITHM TO DISPLAY ALL
		// ELEMENTS IN ARRAY HERE
	}
	
	
}

Uhm i dont see how you can ask us to help add a value to the array in your class if you made it? as far as i see you should call your constructor for the ordered list class then call the insert() method with variable 'v'.. well thats assuming your orderedlist class works

first :

// Array is empty
		if (count == 0)
		{
			// Add first vehicle to list
			array[0] = v;
			count++;
		}
 
		// Array has Vehicles in it
		if (count > 0)
		{

use else if instead since right now if you get in there with count ==0 ; you enter the first if , add the vehicule , increase count , then check if count is > 0 , just over that when u check if count is == max you do a return so you dont need an else if there. so either return after you add the first vehicule, or if you need to do more treatement lower in the method use a else if afterwards.

second :
getDescription should be static to follow your codes logic, since its using "d" which is static, therefor in your main method the call for getDescription shouldn't be made from "v" but from "Vehicule" since it would then be static, BUT the whole static thing here is wrong, having a stating variable called description means each car won't get their own description, instead , there is 1 variable d that is the same for every instances of the Vehicule class. lose the static on d and on setDescription. also dont call getDescription from your constructor, its just returning the null value of "d" into "d", pointless line right there.

public OrderedList(Vehicle v)
	{
		vin = v.getVin();
	}

dont use this, this is your constructor, you want to instanciate your list here, not start filling it. This is where you will receive let's say "max" and initiate your array to that size. also , I would completly remove the "vin" variable from that class (OrderedList)

The reason I would remove it is it's causing you to be confused later on in the insert method, first though, the logical structure of your insert is flawed, I didnt completly follow why you were moving Vehicules arround the array while looking for a duplicate.

You will want to rethink that method, re-write the pseudocode, and then translate it to java. Here is what i think should be your steps to inserting a new vehicule :

public bool insert(Vehicule v){
     bool success = false;

     if( array is full ){
           return success;
     }
     else if( !findVehicule(v) ){
           array[count++]=v;
           sortArray();
           success = true;
     }   
     return success;
}

note that the whole bool returning thing is just extra, it would still work as a void method and with your printlines.

this also assumes you program the find and sort methods.


now here is why i would lose the vin variable in OrderedList :

// Duplicate
if (vin == array[i].getVin())
{	System.out.println("Vehicle with same VIN is already in the

that is false, because the only time you set the vin variable is in the constructor, so it does not represent the actual vin of the v variable you're trying to insert it represents the vin of the v variable you gave the constuctor when you instantiated the OrderedList.

drop the variable and when your running through the array (from 0 to count-1 , not backwards like you do now for x reason)(Although if you do keep your list ordered and it gets big enough binary search would be more efficient than linear search) then compare the array's vehicule's vin with v.getVin() instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.