Results 1 to 14 of 14

Thread: unexpected type

  1. #1
    one
    one is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default unexpected type

    hello all!

    hopefully this hasn't been covered but i was unable to find any threads that were of help to my situation. I am using BlueJ if that's of any use.

    i declared this at the start of the program

    Java Code:
    private int index;
    pages is an array of type Webpage (defined as name and hits - called with getName() and getHits() )

    size is also declared

    Java Code:
       public int find(String pageName)
       {
          
          for (int n = 0; n < size - 1; n++)
          {
              if ( pageName.equals( (pages[n]).getName()) )
              {
                  n = index;
              }
              else
              {
                  -1 = index;
              }
          }
          return index;
       }
    my problem lies in -1 = index; when i try to compile as unexpected type

    thanks for having a look!

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    you cannot write
    -1=index;
    write instead
    index=-1;

  3. #3
    one
    one is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    /facepalm

    thanks :)

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    By the way, you will return -1 except when pageName matches the second to the last entry in the list. The reason is that you don't stop when you find a match. Subsequent checks reset index to -1. Also, n < size - 1 will stop at the second to the last entry of your array.

    Java Code:
    public int find(final String pageName) {
      int index = -1;
      for (n = 0; n < pages.length; n++) {
        if (pageName.equals(pages[n].getName()) {
          index = n;
          break;
        }
      }
    }
    Generally, set the "not found" value of a field such as index once, at the start.

    "break" causes the current loop to stop. The way I coded the loop, the "break" isn't necessary, but it prevents looping for no reason.

  5. #5
    one
    one is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    steve, why did you put final in

    Java Code:
    public int find(final String pageName)
    i removed it and it works

    just had to add a return index; at the end and its working. thanks a lot!

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    The "final" keyword means that the field will not be modified, which is enforced at compile time. It makes the code slightly more efficient, and it promotes good habits, such as not changing the values of input parameters.

    The only reason "final" would cause a problem is if you assigned pageName during the method.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Steve11235 View Post
    The only reason "final" would cause a problem is if you assigned pageName during the method.
    But since String is immutable...

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    But the reference to it is not. (Smug smile) The real purpose of marking parameters final is that, far down the method, a developer can be sure the parameter still references the original value. It's more important with multiple codes working on the same code.

    I mentioned on another post a few minutes ago that I am a code formatting freak. This is just an expression of it. But the use of final does promote good style.
    Last edited by Steve11235; 01-20-2009 at 03:47 AM. Reason: Add some more explanation

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Steve11235 View Post
    But the reference to it is not. (Smug smile)
    Sorry to be a stickler (but most programmer types are a-retentive to a degree, and I'm no exception), but this confuses me a bit. I know what reference vs object means, but just what do you mean by this here? The only thing I know that the final will do will be to not allow a change of the reference of String variable inside of the method. Outside of the method, the reference will still point to the original String no matter what happens inside of the method, even without the final, even if String is changed inside of the method.

  10. #10
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    One thing that should be avoided is modifying the values of parameters within a method. The reason is simply that doing so can lead to confusion if a parameter is modified early on in the method, and the parameter is again accessed later on.

    For example, if a String parameter is null, replacing it with "NA" obscures the fact that null was passed. If someone later on checks the parameter for null or "", the check won't work as expected.

    The use of final is more important with instance variables. The compiler checks that the variables were initialized once. After that, they cannot be changed. This is useful in ensuring that something that should be set once and left alone is not accidentally changed.

    In Eclipse, the clean-up preferences for Java can be set to "final" everything that is not explicitly modified, so I am not just making this up.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks for the explanation. If I understand you correctly, you're talking about changes that affect the variable only from within the method. If so, then I agree whole-heartedly .

  12. #12
    one
    one is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    Steve: a quick question on break;

    im trying to break a loop after the first index that meets the condition is found, as every index after will meet the condition too.

    the way my program is set up is

    for { // a
    if {
    for { } // b
    for { } // c
    --break--
    }
    }

    and i want to break the first for loop (a) after the if is met and both for loops are finished. where would i put the break, after the third for (c) but inside the if?

    looking back, the "diagram" is poor, i will also attach the actual code. thanks!

    Java Code:
       private void insertInSortedArray(WebPage p)
       {
          for (int n = 0; n < pages.length; n++)
          {
              if ( p.getHits() > (pages[n]).getHits() ) 
              {
                  tempArray[n] = p;
                  for (int i = n; i < pages.length; i++)
                  {
                    tempArray[i+1] = pages[i];
                  }
                  for (int i = n+1; i < pages.length; i++)
                  {
                      pages[i] = tempArray[i];
                  }
                  pages[n] = p;
                  break;
               }
           }
       }
    i realize there might still be problems with the code itself, but its no where near complete, so bear with me ;)

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Your question is still not clear, as in the code you posted, the break is within only the outer loop so it doesn't require any fancy treatment.

    Two ways to break out of an inner loop, regardless of the level of nesting:

    1. (Not recommended) Use the form of break that uses a label. See Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics).

    2. (Recommended) Write a method that encapsulates the nested loops. Code a return from the innermost loop when the conditions are met. To satisfy the compiler, you will need an unconditional return statement at the end of the method. If the return type isn't void, consider carefully what you should return in each case.

    db

  14. #14
    one
    one is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    thanks for the quick response

    to clarify; i want to exit the outer-most loop

    for (int n = 0; n < pages.length; n++)

    after the if statement is met.

    i want the if to execute both for loops, update pages[n] with "p" and then stop the outer-most loop.

    Edit: read the link, i believe i have the break in the correct place, but a confirmation would be perfect (as the code isnt finished and compiling/testing isn't possible)
    Last edited by one; 01-20-2009 at 09:35 AM.

Similar Threads

  1. Error: unexpected type
    By silvia in forum New To Java
    Replies: 3
    Last Post: 02-05-2010, 09:54 PM
  2. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 06:24 PM
  3. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  4. Unexpected Error
    By Mir in forum New To Java
    Replies: 10
    Last Post: 07-07-2008, 07:57 PM
  5. An unexpected jumper bug on my IO code?
    By cruxblack in forum New To Java
    Replies: 7
    Last Post: 07-29-2007, 08:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •