views:

108

answers:

5

My last return statement does not work can anyone tell me why? I am completly lost!

public class IpAddress
{

private String dottedDecimal;
private int[] Octet= new int[4];
public int i;
//Default constructor

public IpAddress()
{
  dottedDecimal = "0.0.0.0";
  for ( i=0; i<=3;i++)
   {
    Octet[i] = 0;  //setting the array to zero
   }
} // end default constructor

//************************************
//Specified constructor
 public IpAddress(String myAddress)
 {

   dottedDecimal = myAddress;
   String[] parsedOctets;
   parsedOctets = myAddress.split("\\Q.\\E"); // allows to stop at the dot
   for (i=0; i <=3; i++)
   {
       Octet[i] = Integer.parseInt(parsedOctets[i]); // breaking the string down to integers
   }
 }// end specified constructor

//*****************************************************
public String getDottedDecimal()
 {
    return dottedDecimal;
 }

 //*********************
public int getOctet()
 {
     for (this.i=0; this.i <=3; this.i++)
        {
        return this.Octet[i];
        }

 }
} // end ip address class
+3  A: 

Looks like homework to me, but this is obviously not going to work:

public int getOctet()
{
    for (this.i=0; this.i <=3; this.i++)
    {
      return this.Octet[i];
    }

}

That will return from the function after the first iteration, you can't return from a function multiple times.

Ed Swangren
in some cases you could do a yield return if the return type were IEnumerable
Chris Ballance
Well, yes, but that is not the case here.
Ed Swangren
A: 

For your last statement, you're running a loop that does nothing but return a value. It'll return the first time, but then the loop stops running, so the other returns don't do anything.

public int getOctet()
 {
     for (this.i=0; this.i <=3; this.i++)
        {
          return this.Octet[i];
        }

}

This is equivalent to:

          return this.Octet[0];

Since it only runs once.

Colen
A: 

If you mean the 'get octet' function, it will only ever return Octet[0];

The return statement stops execution of the function the first time it is hit. What you really want is:

public int[] getOctet()
 {
          return this.Octet;
 }
Erich
this did not work i tried i think it was because i was looking for an array. ooh rah great try i appreciated it alot.
daddycardona
+2  A: 

I think what you want for that last method is this:

public int getOctet( int which )
{
    if ( which >= 0 && which <= 3 )
        return this.Octet[ which ];
    else
        return -1;  // error. Consider throwing an exception
}
Bob Kaufman
this is what i was looking for I think i understand what you are saying, your saying in this statement that 0 thru 3 are acceptable because i only have objects in this array but anything else is a negative one cause it is false. I just learned this parseint stuff as well you guys are all awesome.
daddycardona
A: 

By naming your method getOctect(), it seems that you are hoping to return the array, not an individual int from within the array. In that case, you should simplify it to something like this:

public int[] getOctet()
{
      return this.Octet;

}
akf