for loop

I need to make a for loop to print out a list of items

Page 1 of 1

8 Replies - 1498 Views - Last Post: 04 August 2007 - 01:20 PM Rate Topic: -----

#1 bigbluesky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-July 07

for loop

Post icon  Posted 02 August 2007 - 04:12 PM

I am unsure how to make a for loop for items instead of numbers.

for example how would I make a for loop to print this list one item at a time?

 inventory[0] = new Product("Books", 1, 1000, 6.99);
  inventory[1] = new Product("Bookmarks", 2, 600, 2.99);
  inventory[2] = new Product("Book Lights", 3, 500, 10.99);
  inventory[3] = new Product("Book Plates", 4, 800, 3.99);
  inventory[4] = new Product("Book Ends", 5, 200, 5.99);


I tried:

for (new Product() )

System.printf (new Product() )


and lots of other things I could think of - nothing works.

Thank you for your time.

-bigbluesky

Is This A Good Question/Topic? 0
  • +

Replies To: for loop

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4438
  • View blog
  • Posts: 12,308
  • Joined: 18-April 07

Re: for loop

Posted 02 August 2007 - 04:31 PM

You can use what is called an enhanced for loop like this...

for (Product item : inventory) {
	 //do something with an item
	 item.doSomething();
}



That will allow you to loop through the array of Products. Great for collections and such as well.

Edit: I was born in Montana... Great Falls. Yeeeee haaaa! ;)

Enjoy!

This post has been edited by Martyr2: 02 August 2007 - 04:32 PM

Was This Post Helpful? 0
  • +
  • -

#3 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: for loop

Posted 02 August 2007 - 04:35 PM

Hi, well did you look at the loop I gave you in the other thread?
for(int i=0; i<3; i++)
System.out.println(inventory[i].toString());


or if the user enters n inventories
then just change the condition to i<n
for(int i=0; i<n; i++)
System.out.println(inventory[i].toString());


is this what you're looking for?
if not please say so.

O and yeah I heard about the bridge crash ... just terrible
we have a discusion about it HERE in the lounge.
Was This Post Helpful? 0
  • +
  • -

#4 bigbluesky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-July 07

Re: for loop

Posted 02 August 2007 - 06:19 PM

View PostMartyr2, on 2 Aug, 2007 - 04:31 PM, said:

You can use what is called an enhanced for loop like this...

for (Product item : inventory) {
	 //do something with an item
	 item.doSomething();
}



That will allow you to loop through the array of Products. Great for collections and such as well.

Edit: I was born in Montana... Great Falls. Yeeeee haaaa! ;)

Enjoy!



Thank you!
and Great Falls is awesome!
I have lived in MT since '82 - was born in Sandpoint, ID myself.
you knew where I was from by the screen name, right?
I live in Stevensville, south of Missoula.

-bigbluesky

View PostPennyBoki, on 2 Aug, 2007 - 04:35 PM, said:

Hi, well did you look at the loop I gave you in the other thread?
for(int i=0; i<3; i++)
System.out.println(inventory[i].toString());


or if the user enters n inventories
then just change the condition to i<n
for(int i=0; i<n; i++)
System.out.println(inventory[i].toString());


is this what you're looking for?
if not please say so.

O and yeah I heard about the bridge crash ... just terrible
we have a discusion about it HERE in the lounge.



Thank you - I think that is just what I need, from what I see in the book and from what my prof is saying - nice to have it confirmed again - I will look at that last file you made again also. :)

-bigbluesky
Was This Post Helpful? 0
  • +
  • -

#5 bigbluesky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-July 07

Re: for loop

Posted 02 August 2007 - 06:29 PM

View Postbigbluesky, on 2 Aug, 2007 - 06:19 PM, said:

View PostMartyr2, on 2 Aug, 2007 - 04:31 PM, said:

You can use what is called an enhanced for loop like this...

for (Product item : inventory) {
	 //do something with an item
	 item.doSomething();
}



That will allow you to loop through the array of Products. Great for collections and such as well.

Edit: I was born in Montana... Great Falls. Yeeeee haaaa! ;)

Enjoy!



Thank you!
and Great Falls is awesome!
I have lived in MT since '82 - was born in Sandpoint, ID myself.
you knew where I was from by the screen name, right?
I live in Stevensville, south of Missoula.

-bigbluesky

View PostPennyBoki, on 2 Aug, 2007 - 04:35 PM, said:

Hi, well did you look at the loop I gave you in the other thread?
for(int i=0; i<3; i++)
System.out.println(inventory[i].toString());


or if the user enters n inventories
then just change the condition to i<n
for(int i=0; i<n; i++)
System.out.println(inventory[i].toString());


is this what you're looking for?
if not please say so.

O and yeah I heard about the bridge crash ... just terrible
we have a discusion about it HERE in the lounge.



Thank you - I think that is just what I need, from what I see in the book and from what my prof is saying - nice to have it confirmed again - I will look at that last file you made again also. :)

-bigbluesky



I feel good about this now - I wanted to understand it myself - I have been at this almost all day - I finally get it! I didn't want to just turn in work done by someone as when I didn't understand it! Now I do and I totally agree with the code . . . I could have written it myself!

The only thing I have to get to work now is total inventory value - and my prof has the code for that - I hope I can get it to work - this programming stuff is hard for me - it takes me a bit longer than for most people I think, Thank you for being patient!

-bigbluesky
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4438
  • View blog
  • Posts: 12,308
  • Joined: 18-April 07

Re: for loop

Posted 02 August 2007 - 07:20 PM

Yeah I knew you were from Montana through the screen name and your sig. After all, the state is known as the big sky country.

This post has been edited by Martyr2: 02 August 2007 - 07:21 PM

Was This Post Helpful? 0
  • +
  • -

#7 bigbluesky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-July 07

Re: for loop

Posted 04 August 2007 - 12:51 PM

View PostMartyr2, on 2 Aug, 2007 - 07:20 PM, said:

Yeah I knew you were from Montana through the screen name and your sig. After all, the state is known as the big sky country.



I have one more question about this program.
I don't know why I could not get the total value of the inventory with the following code pasted into the product class in my information file (Product.java):


//Calculates and returns totatInventoryValue in dollars
	public static double totalInventoryValue( Product inventory[] )
	{
	  double value = 0.0;

	  for(Product p: inventory)
	  {
		value += p.stockValueInDollars();

	  }//End for

	  return( value );

	}//End method totalInventoryValue


Did I need to have some other code along with this to get it to work in the main file (Inventory2.java)?

Thank you!

-bigbluesky
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4438
  • View blog
  • Posts: 12,308
  • Joined: 18-April 07

Re: for loop

Posted 04 August 2007 - 01:04 PM

Well other than just using return value; instead of using the parenthesis, it looks good. What error are you getting? I would look to the make sure you are using the correct case with your method back in your Product class. Make sure they match.

Post the error so we can see it if you are getting one. If you don't get an error and instead just get an incorrect value, then its the logic back in your Product class that needs to be fixed.

:)
Was This Post Helpful? 0
  • +
  • -

#9 bigbluesky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-July 07

Re: for loop

Posted 04 August 2007 - 01:20 PM

View PostMartyr2, on 4 Aug, 2007 - 01:04 PM, said:

Well other than just using return value; instead of using the parenthesis, it looks good. What error are you getting? I would look to the make sure you are using the correct case with your method back in your Product class. Make sure they match.

Post the error so we can see it if you are getting one. If you don't get an error and instead just get an incorrect value, then its the logic back in your Product class that needs to be fixed.

:)



I don't get an error or an incorrect value - it just seems to ignore that part of the code and does nothing with it at all. The output for the rest of the code works - it just doesn't give me a total value for the inventory. I know I am doing something wrong tho. LOL

Here is my code:

//Product.java

public class Product implements Comparable
{
	//Private Variables

	private String name;			   //Product name
	private int identificationNumber;  //The product identification number
	private int unitsInStock;		  //Number of units in stock
	private double unitPriceInDollars; //The price of each unit in dollars


	//Constructors

	//Default constructor
	public Product()
	{
		//Set some default values for each private variable
		name				  = "";
		identificationNumber  = 0;
		unitsInStock		  = 0;
		unitPriceInDollars	= 0.0;

	}//End Inventory constructor

	Product inventory [] = new Product [3];

	//Initialization constructor
	public Product( String nameIn, int identificationNumberIn,
					int unitsInStockIn, double unitPriceInDollarsIn )
	{

		//Set the item name--use the set methods to enforce bounds checking
		setName( nameIn );

		//Set the item identification number
		setIdentificationNumber( identificationNumberIn );

		//Set the number of units in stock
		setUnitsInStock( unitsInStockIn );

		//Set the unit price in dollars
		setUnitPriceInDollars( unitPriceInDollarsIn );

	}//End Inventory constructor


	//Public Methods

	//Set Methods


	//Stores the item name
	public void setName( String nameIn )
	{
		name = nameIn; //Store the item name

	}//End setName


	//Stores the product identification number
	public void setIdentificationNumber( int identificationNumberIn )
	{
		//Store the item identification number
		// If the value is negative, then store 0
		identificationNumber = ( (identificationNumberIn > 0) ? identificationNumberIn : 0 );

	}//End setID

	//Stores the number of units in stock
	public void setUnitsInStock( int unitsInStockIn )
	{
		//Store the number of units in stock
		// If the value is negative, then store 0
		unitsInStock = ( (unitsInStockIn > 0)?unitsInStockIn:0 );

	}//End setUnitsInStock

	//Stores the price of each unit in dollars
	public void setUnitPriceInDollars( double unitPriceInDollarsIn )
	{
		//Store the unit price
		// If the value is negative, then store 0.0
		unitPriceInDollars = ( (unitPriceInDollarsIn > 0.0)?unitPriceInDollarsIn:0.0);

	}//End setUnitPriceInDollars


	//Get Methods

	//Returns the item name
	public String getName()
	{
		return ( name ); //Return the identification name

	} // End getName

	//Returns the item identification number
	public int getIdentificationNumber()
	{
		return ( identificationNumber ); //Return the identification number

	}//End getIdentificationNumber

	//Returns the number of units in stock
	public int getUnitsInStock()
	{
		return( unitsInStock ); //Return the number of units in stock

	}//End getUnitsInStock

	//Returns the price of each unit
	public double getUnitPriceInDollars()
	{
		return( unitPriceInDollars ); //Return the unit price

	}//End getUnitPriceInDollars

	//Other Methods

	//Returns the total value of the inventory in dollars
	public double stockValueInDollars()
	{
		return ( unitsInStock * unitPriceInDollars );

	}//End stockValueInDollars

	public static double totalInventoryValue( Product inventory[] )
	{
	  double value = 0.0;
	  for(Product p: inventory)
	  {
	   value += p.stockValueInDollars();
	  }//End for
	  return( value );
	}//End method totalInventoryValue

	//Overloads the String objects compareTo() method so Products can be
	//compared and potentially sorted by name.
	public int compareTo (Object o)
	{
		String s2 = ((Product)o).name.toUpperCase();
		String s1 = name.toUpperCase();
		return ( s1.compareTo(s2) );

	 } //End compareTo


	//Returns a formatted String for output purposes
	public String toString()
	{
		String formatString = "Identification Number : %d\n";
		formatString	   += "Product			   : %s\n";
		formatString	   += "Units In Stock		: %d\n";
		formatString	   += "Unit Price			: $%.2f\n";
		formatString	   += "Stock Value		   : $%.2f\n\n";

		return ( String.format( formatString, identificationNumber, name, unitsInStock,
								unitPriceInDollars, stockValueInDollars())
			   );

	}//End toString()

}//End class Product



import java.util.Arrays; //For Arrays.sort

public class Inventory2
{
	public static void main( String args[] )
	{
//Declare and initialize the inventory array
  Product inventory []; //An array variable

  //Size the array
  inventory = new Product[3];

  inventory[0] = new Product("Books", 1, 1000, 6.99);
  inventory[1] = new Product("Bookmarks", 2, 800, 2.99);
  inventory[2] = new Product("Book Lights", 3, 300, 10.99);

//Sort the array
Arrays.sort( inventory );

int x = inventory[0].compareTo(inventory[1]);

System.out.println(x+"\n");

for(int i=0; i<3; i++)
System.out.println(inventory[i].toString());

}//End main

}//End Inventory2



Do I have it in the wrong file perhaps or is there a code to "call" it to the other file to be used? I think I am missing something . . .

Thanks,

-bigbluesky
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1