Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: exercise 168 and apologies for my last post

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default exercise 168 and apologies for my last post

    Hi, I'm sorry about the last post. I shouldn't just say "I don't get it" as the whole question. It's totally my fault. Anyways, I am having a problem with this code. I get an error no matter what I try. The error I'm getting right now is this:

    "error cannot find symbol"

    So, here is the code:

    public class Item
    {
      private int myN;
     
      public Item( int n )
      {
        myN = n;
      }
     
      public String toString()
      {
        return "Item: " + myN;
      }
     
      public int getN() 
      {
        return myN;
      }
     
      public static Item[] makeItemArray( int len )
      {
        Item[] a = new Item[ len ];
        int i;
        for ( i = 0 ; i < len ; i++ )
          a[ i ] = new Item( i );
        return a;
      }
    }
     
    public class MainClass
    {
      public static void main( String[] args )
      {
     
    //this is the part where I can edit the code:
     
        Item[] array = Item.makeItemArray( 10 );
        int nEvens = 0;
     
         for (Item item : array)
     
          for (i = 0; i < array.length - 1; i++){
     
              if (i % 2 == 0){
     
                  nEvens++;
     
              }
     
          }
     
        System.out.println( "There are " + nEvens + " evens." );
     
    //that was it
      } 
    }

    I thought I was on the right track with the nested for-loops, but I'm not quite sure.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: exercise 168 and apologies for my last post

    look at your loops:
    for (i = 0; i < array.length - 1; i++){

    you did not declare a variable i that is why you end up
    with compilation error cannot find symbol.
    declare the type of that variable first before the naming.

    for(int i = 0; i < .......... something like that.
    kindly see this link for tutorial of loops:
    Java Loops - for, while and do...while

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: exercise 168 and apologies for my last post

    Hey, I made it work. Thanks for referring me to the loops tutorial. I really appreciate it. I'll read it. Here is my code:

     
    public class Item
    {
      private int myN;
     
      public Item( int n )
      {
        myN = n;
      }
     
      public String toString()
      {
        return "Item: " + myN;
      }
     
      public int getN() 
      {
        return myN;
      }
     
      public static Item[] makeItemArray( int len )
      {
        Item[] a = new Item[ len ];
        int i;
        for ( i = 0 ; i < len ; i++ )
          a[ i ] = new Item( i );
        return a;
      }
    }
     
    public class MainClass
    {
      public static void main( String[] args )
      {
        Item[] array = Item.makeItemArray( 10 );
        int nEvens = 0;
     
        public static void main( String[] args )
     
    {
     
      Item[] array = Item.makeItemArray( 12 );
     
      int nEvens = 0;
     
      for (Item item : array)
     
          for (int i = 0; i < array.length; i++){
     
              if (i % 2 == 0){
     
                  nEvens = i / 2;
     
              }
     
          }
     
          nEvens++;
     
      System.out.println( "There are " + nEvens + " evens." );
     
    }
     
        System.out.println( "There are " + nEvens + " evens." );
      } 
    }

    That worked perfectly.

    --- Update ---

    Thanks for pointing out the syntax error as well.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Location
    California
    Posts
    9
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: exercise 168 and apologies for my last post

    Your code compiles and runs for me with one minor fix.
    The inner loop in MainClass needs to have it's index (i)
    declared. Try for (int i = 0;...
    instead of for (i = 0;...

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: exercise 168 and apologies for my last post

    @mageorge

    Oh, no I know. I fixed the "i" to "int i" and it totally almost made it work. I didn't have to change much after that, besides make nEvens = i / 2; and increment nEvens after the loop was done. That was, until I noticed the MainClass. The problem is, the part you mentioned isn't editable code. It's preserved by the textbook. Thanks though.

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM
  3. Exercise 86
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2013, 11:31 PM
  4. Help with exercise.
    By javapol in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 09:40 PM
  5. Exercise
    By phite2009 in forum Member Introductions
    Replies: 3
    Last Post: September 30th, 2011, 08:51 AM

Tags for this Thread