Welcome to Dream.In.Code
Become a Java Expert!

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!




for loops, if statements and {}

2 Pages V  1 2 >  
Reply to this topicStart new topic

for loops, if statements and {}

snake
5 Jul, 2007 - 02:55 PM
Post #1

New D.I.C Head
*

Joined: 14 Jun, 2007
Posts: 31


My Contributions
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
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: For Loops, If Statements And {}
5 Jul, 2007 - 02:56 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,123



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
How do you check for/use the true & false values?
User is online!Profile CardPM
+Quote Post

snake
RE: For Loops, If Statements And {}
5 Jul, 2007 - 02:59 PM
Post #3

New D.I.C Head
*

Joined: 14 Jun, 2007
Posts: 31


My Contributions
I have no idea what you are talking about.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: For Loops, If Statements And {}
5 Jul, 2007 - 03:02 PM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,123



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(snake @ 5 Jul, 2007 - 03:59 PM) *

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?
User is online!Profile CardPM
+Quote Post

snake
RE: For Loops, If Statements And {}
5 Jul, 2007 - 03:04 PM
Post #5

New D.I.C Head
*

Joined: 14 Jun, 2007
Posts: 31


My Contributions
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
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: For Loops, If Statements And {}
6 Jul, 2007 - 01:31 AM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
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
User is offlineProfile CardPM
+Quote Post

snake
RE: For Loops, If Statements And {}
6 Jul, 2007 - 02:27 AM
Post #7

New D.I.C Head
*

Joined: 14 Jun, 2007
Posts: 31


My Contributions
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;} ????

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: For Loops, If Statements And {}
6 Jul, 2007 - 02:51 AM
Post #8

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Try this
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;}
              
             else
            {
                     return false;
            }
        }
    }



User is offlineProfile CardPM
+Quote Post

snake
RE: For Loops, If Statements And {}
6 Jul, 2007 - 04:59 AM
Post #9

New D.I.C Head
*

Joined: 14 Jun, 2007
Posts: 31


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: For Loops, If Statements And {}
6 Jul, 2007 - 05:14 AM
Post #10

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,123



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(snake @ 6 Jul, 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.

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.
User is online!Profile CardPM
+Quote Post

snake
RE: For Loops, If Statements And {}
6 Jul, 2007 - 05:31 AM
Post #11

New D.I.C Head
*

Joined: 14 Jun, 2007
Posts: 31


My Contributions
Here is the entire class.


CODE

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;
    }    
}

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: For Loops, If Statements And {}
6 Jul, 2007 - 06:39 AM
Post #12

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
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;
    }

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.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:26PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month