Results 1 to 7 of 7
  1. #1
    kpro862 is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default trouble with array method

    i'm almost 100% complete with the array method that i'm writing. but i'm having trouble.

    i wrote a method that returns a string ("up_order", "down_order", "no_order") if the elements in the integer array correspond.

    method code:

    Java Code:
         String order;
    
         for(int i=0;i<list.length;i++)
         {
              if(i>0 && list[i]>list[i-1])
              {
                   order = "up_order";
              }
              else if(i>0 && list[i-1]>list[i])
              {
                   order="down_order";
              }
              else
              {
                   order="no_order";
              }
         }
         
         return order;
    what's wrong with the code?
    if the return statement is outside the for loop like i have it in the code above, it says that order may have not been initialized. if the return statement is inside the for loop it says i need a return statement. i'm completely lost

    Moderator Edit: Code tags added
    Last edited by Fubarable; 05-12-2010 at 12:22 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Drareeg is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    U might try this:
    replace String order;
    with
    String order = new String;
    just an idea

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

    Default

    Quote Originally Posted by kpro862 View Post
    what's wrong with the code?
    if the return statement is outside the for loop like i have it in the code above, it says that order may have not been initialized. if the return statement is inside the for loop it says i need a return statement. i'm completely lost
    If the return is outside of the loop, then there's no guarantee that the String will be initialized, for instance if length of the array is 0, and so the compiler rightly complains that there's a risk of returning a non-initialized object.

    If the return is inside the loop, then you again run the risk of the loop not looping and return never being reached.

    That being said, even if you get this part right I fear that your code is broken. You may want to walk through your code including the loop as if you were the computer asking what happens when this or that line of code is reached and go through the loop several times. What happens when "i" is 0? What happens if the Strings are increasing all the time except for the last String which is decreasing? What will the output result be? You may need to check the status of increasing or decreasing during the loop (perhaps a boolean would be helpful here?).

    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Please read the link in my signature for more on code tags.

    Much luck!

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

    Default

    Quote Originally Posted by Drareeg View Post
    U might try this:
    replace String order;
    with
    String order = new String;
    just an idea
    I disagree. There are rarely any times you'll want to call new String(...), and this definitely isn't one of them. I'd recommend to fix the logic of the program.
    Last edited by Fubarable; 05-12-2010 at 12:43 AM. Reason: corrected: really changed to "rarely"

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by kpro862 View Post
    i'm almost 100% complete with the array method that i'm writing. but i'm having trouble.

    i wrote a method that returns a string ("up_order", "down_order", "no_order") if the elements in the integer array correspond.

    method code:

    Java Code:
         String order;
    
         for(int i=0;i<list.length;i++)
         {
              if(i>0 && list[i]>list[i-1])
              {
                   order = "up_order";
              }
              else if(i>0 && list[i-1]>list[i])
              {
                   order="down_order";
              }
              else
              {
                   order="no_order";
              }
         }
         
         return order;
    what's wrong with the code?

    only the last two elements in the array determine the result string. example: if you check this list { 2, 4, 1, 3, 5 } in your method, the result is up_order and if you check this list { 2, 4, 1, 5, 3 } the result is down_order. the logic is wrong.

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

    Default

    Quote Originally Posted by j2me64 View Post
    only the last two elements in the array determine the result string. example: if you check this list { 2, 4, 1, 3, 5 } in your method, the result is up_order and if you check this list { 2, 4, 1, 5, 3 } the result is down_order. the logic is wrong.
    Yeah, I was hoping that the OP would figure this out for himself, but this works too.

  7. #7
    kpro862 is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default

    thanks for the help guys. i'll fix up the code when i get home and let you guys know how everything goes.

Similar Threads

  1. Array trouble
    By hobo in forum New To Java
    Replies: 8
    Last Post: 11-17-2009, 01:04 PM
  2. [SOLVED] Trouble a method
    By robocop in forum New To Java
    Replies: 14
    Last Post: 03-04-2009, 12:44 AM
  3. [SOLVED] Method trouble &gt;:0!
    By PureAwesomeness in forum New To Java
    Replies: 10
    Last Post: 02-20-2009, 07:18 AM
  4. Trouble with method
    By BlueJ2008 in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 10:05 PM
  5. Trouble will calling a method
    By jonsamwell in forum New To Java
    Replies: 9
    Last Post: 08-22-2008, 11:16 PM

Posting Permissions

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