for loops, if statements and {}

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2104 Views - Last Post: 07 July 2007 - 03:54 PM Rate Topic: -----

#1 snake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 07

for loops, if statements and {}

Posted 05 July 2007 - 03:55 PM

I am getting an error: It is a good idea to always enclose the code in the body of an if statement. I have tried many other places for the braces, but nothing seems to help. This will compile in BlueJ but I loose point, and I need all the points I can get in my class.
	public boolean postalZoneExists(int zipCode)
	{
		// loop POPULATED element number of times and compare 
		// value in postalZoneArray to the input value and
		// return true as soon as it is found.		
		for ( int i = 0; i < nextPostalZone; ++i )
		{
			if ( postalZoneArray[i].getZipCode() == zipCode )		   
				return true;
		}	  
		// we only reach here if not found
		{
			return false;
		}
	}


This post has been edited by snake: 05 July 2007 - 03:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: for loops, if statements and {}

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6643
  • View blog
  • Posts: 30,932
  • Joined: 10-May 07

Re: for loops, if statements and {}

Posted 05 July 2007 - 03:56 PM

How do you check for/use the true & false values?
Was This Post Helpful? 0
  • +
  • -

#3 snake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 07

Re: for loops, if statements and {}

Posted 05 July 2007 - 03:59 PM

I have no idea what you are talking about.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6643
  • View blog
  • Posts: 30,932
  • Joined: 10-May 07

Re: for loops, if statements and {}

Posted 05 July 2007 - 04:02 PM

View Postsnake, on 5 Jul, 2007 - 03:59 PM, said:

I have no idea what you are talking about.

Based on the results of checking the array vs the zip code value, you return true or false.

If you return false, how does the calling function deal with the true or false values returned from the fuciton postalZoneExists?
Was This Post Helpful? 0
  • +
  • -

#5 snake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 07

Re: for loops, if statements and {}

Posted 05 July 2007 - 04:04 PM

Not sure exactly but here is some more of my project.
	/** postalZoneExists - indicates whether PostalZone exists.
	 * 
	 * @param  zipCode int zipCode of PostalZone to find
	 * 
	 * @return	 boolean  true is zipZone exists in zipArray, 
	 *false otherwise
	 */
	public boolean postalZoneExists(int zipCode)
	{
		// loop POPULATED element number of times and compare 
		// value in postalZoneArray to the input value and
		// return true as soon as it is found.		
		for ( int i = 0; i < nextPostalZone; ++i )
		{
			if ( postalZoneArray[i].getZipCode() == zipCode )		   
				return true;
		}	  
		// we only reach here if not found
		{
			return false;
		}
	}
	
	/** findPostalZone - find and return PostalZone object. 
	 * 
	 * @param  zipCode int zipCode of ZipZone to find
	 * 
	 * @return	 ZipZone  if found, returns zip zone object,
	 *					  if not found, returns null reference
	 */
	public PostalZone findPostalZone(int zipCode)
	{
		// loop POPULATED element number of times and compare 
		// value in postalZoneArray to the input value and
		// as soon as it is found return that PostalZone object.
		for ( int i = 0; i < nextPostalZone; ++i )
		{
			if ( postalZoneArray[i].getZipCode() == zipCode )
				return postalZoneArray[i];  
		}		
				
		// we only reach here if not found
		{
			return null;
		}
	}   


This post has been edited by snake: 05 July 2007 - 04:16 PM

Was This Post Helpful? 0
  • +
  • -

#6 PennyBoki   User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: for loops, if statements and {}

Posted 06 July 2007 - 02:31 AM

Hi, I'm really not so sure about what are you trying to accomplish here, but I can see that you do not use else in your code which should be instead of the comment //we only reach here if not found.
Can you tell what is the purpose of your function in detail if that's ok

This post has been edited by PennyBoki: 06 July 2007 - 02:33 AM

Was This Post Helpful? 0
  • +
  • -

#7 snake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 07

Re: for loops, if statements and {}

Posted 06 July 2007 - 03:27 AM

This is part of a projuct I am working for an on line class. The instructor sends us a shell and we have to fill in the code. based on the comments he gives us. Everything compiles, he has said it looks good and acts the way it is supposed to. I submitt for grading to a web site that checks for test classes, method execution, style and lots of other stuff. For some reason the web site is saying I need more Braces{}.
I have pondered the else statement but unable to make it work. Should it look something like: return true;}{ else }{return false;} ????
Was This Post Helpful? 0
  • +
  • -

#8 PennyBoki   User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: for loops, if statements and {}

Posted 06 July 2007 - 03:51 AM

Try this
 public boolean postalZoneExists(int zipCode)
	{
		// loop POPULATED element number of times and compare
		// value in postalZoneArray to the input value and
		// return true as soon as it is found.		
		for ( int i = 0; i < nextPostalZone; ++i )
		{
			if ( postalZoneArray[i].getZipCode() == zipCode )		  
				{return true;}
			  
			 else
			{
					 return false;
			}
		}
	}

Was This Post Helpful? 0
  • +
  • -

#9 snake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 07

Re: for loops, if statements and {}

Posted 06 July 2007 - 05:59 AM

As usual when I tried this I get a different set of errors. It tells me that at the last } I am missing a return statemen.
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6643
  • View blog
  • Posts: 30,932
  • Joined: 10-May 07

Re: for loops, if statements and {}

Posted 06 July 2007 - 06:14 AM

View Postsnake, on 6 Jul, 2007 - 05:59 AM, said:

As usual when I tried this I get a different set of errors. It tells me that at the last } I am missing a return statemen.

Chances are you have an extra open bracket somewhere else in your code.

Either post all of your program, or download a text editor that can show you matching brackets, like crimsoneditor.
Was This Post Helpful? 0
  • +
  • -

#11 snake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 07

Re: for loops, if statements and {}

Posted 06 July 2007 - 06:31 AM

Here is the entire class.


import Lab4Support.*;

/**
 * Locator - provides data for stores and zip code zones and methods
 *	  to find closest store based on zip code.
 * 
 * @author John Fulton 
 * @version 1.0
 */
public class Locator
{
	// array size (and limit)
	private final static int MAX_ENTRIES = 100;   
	// instance variables	
	// array of store objects
	Store [] storeArray;	
	// array of postal zone objects
	PostalZone [] postalZoneArray;	
	// next available store array position
	private int nextStore;	
	// next available postal zone position
	private int nextPostalZone;	
	/**
	 * Constructor for objects of class Locator.
	 */
	public Locator()
	{
		this.storeArray = new Store[MAX_ENTRIES];
		postalZoneArray = new PostalZone[MAX_ENTRIES];
	} 
	/**
	 * addStore - add a store to the array of stores.
	 * @param inputStoreNumber int Store number to add
	 * @param inputDescription String Store description to add
	 * @param inputLocation String Location of store
	 * @return int  index of added store, -1 if unable to
	 *				  add (array already full)
	 */
	public int addStore(int inputStoreNumber, String inputDescription,
		Location inputLocation)
	{
		if (this.nextStore < MAX_ENTRIES)
		{
			Store tempStore = new Store(inputStoreNumber, inputDescription,
				inputLocation);
			this.storeArray[this.nextStore] = tempStore;
			this.nextStore++;
			return (this.nextStore - 1);  
		}
		else
		{
			// array is full
			return -1;
		}
	}	
	/**
	 * addStore - add a store to the array of stores.
	 * 
	 * @param  inputStore Store   zipZone to be added
	 * @return	 int  index of added store, -1 if unable to
	 *				  add (array already full)
	 */
	public int addStore(Store inputStore)
	{
		if (this.nextStore < MAX_ENTRIES)
		{
			this.storeArray[this.nextStore] = inputStore;
			this.nextStore++;
			return (this.nextStore - 1);  
		}
		else
		{
			// array is full
			return -1;
		}
	}	
	/**
	 * addPostalZone - add a zip code zone to the array of PostalZones.
	 * 
	 * @param   inputPostalZone int zip code of zone to add
	 * @param   inputLocation  Location  location object for zipZone to add
	 * 
	 * @return  int  index of added zipZone, -1 if unable to
	 *				  add (array already full)
	 */
	public int addPostalZone(int inputPostalZone, Location inputLocation)
	{
		return addPostalZone( new PostalZone( inputPostalZone,
			inputLocation ) );
	}   
	/**
	 * addPostalZone - add a zip code zone to the array of PostalZones.
	 * 
	 * @param  inputPostalZone PostalZone   zipZone to be added
	 * 
	 * @return	 int  index of added zipZone, -1 if unable to
	 *				  add (array already full)
	 */
	public int addPostalZone(PostalZone inputPostalZone)
	{
		if (this.nextPostalZone < MAX_ENTRIES)
		{
			this.postalZoneArray[this.nextPostalZone] = inputPostalZone;
			this.nextPostalZone++;
			return (this.nextPostalZone - 1);  
		}
		else
		{
			// array is full
			return -1;
		}
	}	
	/** postalZoneExists - indicates whether PostalZone exists.
	 * 
	 * @param  zipCode int zipCode of PostalZone to find
	 * 
	 * @return	 boolean  true is zipZone exists in zipArray, 
	 *					  false otherwise
	 */
	public boolean postalZoneExists(int zipCode)
	{
		// loop POPULATED element number of times and compare 
		// value in postalZoneArray to the input value and
		// return true as soon as it is found.		
		for ( int i = 0; i < nextPostalZone; ++i )
		{
			if ( postalZoneArray[i].getZipCode() == zipCode )
			{
				return true;
			}	  
		// we only reach here if not found
			   else
			   {
				return false;
			   }
		}	
	}
	
	/** findPostalZone - find and return PostalZone object. 
	 * 
	 * @param  zipCode int zipCode of ZipZone to find
	 * 
	 * @return	 ZipZone  if found, returns zip zone object,
	 *					  if not found, returns null reference
	 */
	public PostalZone findPostalZone(int zipCode)
	{
		// loop POPULATED element number of times and compare 
		// value in postalZoneArray to the input value and
		// as soon as it is found return that PostalZone object.
		for ( int i = 0; i < nextPostalZone; ++i )
		{
			if ( postalZoneArray[i].getZipCode() == zipCode )
			{
				return postalZoneArray[i];  
			}								
		// we only reach here if not found
			   else
			   {
				return null;
			   }
		}	
	}   
	/** toString - overrides method in base Object class.
	 * 
	 * @return	 string   values stored in Store and PostalZone arrays, 
	 *					  formatted for printing
	 */
	public String toString()
	{
		String msg;
		msg = "\n\nStores:\n";
		
		for (int i = 0; i < this.nextStore; i++)
		{
			msg += this.storeArray[i].getStoreNumber() + ", "
					+ this.storeArray[i].getDescription() + ", "
					+ this.storeArray[i].getStoreLocation();
			msg += '\n';
		}
		
		msg += "\n\nZip Code Zones:\n";
		
		for (int i = 0; i < this.nextPostalZone; i++)
		{
			msg += this.postalZoneArray[i].getZipCode() + ", " 
					+ this.postalZoneArray[i].getzipLocation() + "  ";
			msg += '\n';
		}
			  
		return msg;
	}	
}


Was This Post Helpful? 0
  • +
  • -

#12 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: for loops, if statements and {}

Posted 06 July 2007 - 07:39 AM

public boolean postalZoneExists(int zipCode)
	{
		// loop POPULATED element number of times and compare
		// value in postalZoneArray to the input value and
		// return true as soon as it is found.		
		for ( int i = 0; i < nextPostalZone; ++i )
		{
			if ( postalZoneArray[i].getZipCode() == zipCode )		  
				return true;
		 }
			return false;
	}


the logic here is ok pennyboki, there was just an extra set of {} around return false. It searches all elements of the array, if found returns true, other wise, it leaves the for loop and returns false. The addition you made would only check a single element and choose true or false to return.


we need to see the PostalZone class, specifically the getZipCode() method. As this may be where the error is.
Was This Post Helpful? 0
  • +
  • -

#13 snake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 07

Re: for loops, if statements and {}

Posted 06 July 2007 - 08:35 AM

when I submit this without the {} around return false, it still takes away points saying I need to have {} around my if statements.
Was This Post Helpful? 0
  • +
  • -

#14 PennyBoki   User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: for loops, if statements and {}

Posted 06 July 2007 - 08:44 AM

I use the william's code:

public boolean postalZoneExists(int zipCode)
	{
		// loop POPULATED element number of times and compare
		// value in postalZoneArray to the input value and
		// return true as soon as it is found.		
		for ( int i = 0; i < nextPostalZone; ++i )
		{
			if ( postalZoneArray[i].getZipCode() == zipCode )		  
			   { return true;}
		 }
			return false;
	}

Was This Post Helpful? 0
  • +
  • -

#15 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: for loops, if statements and {}

Posted 06 July 2007 - 08:55 AM

it is better to have {} around all if statements in case of expanding beyond 1 command, but most compilers will allow them to be left out. The code above should work perfectly assuming your other classes are working properly.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2