Join 149,521 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,403 people online right now. Registration is fast and FREE... Join Now!
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.
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; } // we only reach here if not found { return false; } }
This post has been edited by snake: 5 Jul, 2007 - 02:59 PM
Not sure exactly but here is some more of my project.
CODE
/** 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: 5 Jul, 2007 - 03:16 PM
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: 6 Jul, 2007 - 01:33 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;} ????
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;}
/** * 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'; }
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.