Results 1 to 10 of 10
  1. #1
    luckyleaf95 is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default BUG:local variable is never read

    Hi
    I'm supposed to correct the code, so when I run the program, it changes all the names to Sally and prints them on the screen. But, this is what I get:
    Contents of Names:
    John
    Bill
    Sheila
    Amit
    Jane
    Changing Names to "Sally"
    Contents of Names:
    John
    Bill
    Sheila
    Amit
    Jane

    Here's the code:



    public class Names {
    private String[] names = { "John", "Bill", "Sheila", "Amit", "Jane" };

    public Names() {
    }

    public void setNames(String name) {
    for (String str : names)
    str = name;

    }

    public void printNames() {
    System.out.println("Contents of Names:");
    for (String s : names) {
    System.out.println("\t" + s);
    }
    }

    public static void main(String[] args) {
    Names n = new Names();
    n.printNames();
    System.out.println("Changing Names to \"Sally\"");
    n.setNames("Sally");
    n.printNames();

    }

    }

    Eclipse says that the setNames method is wrong and that the local variable "str" is never read. I've been working on this for hours and now I am kind of stuck. Can anyone please tell me where the problem is? Thanks in advance

  2. #2
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    are you sure. i just compiled the code . it compiles successfully

  3. #3
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Quote Originally Posted by luckyleaf95 View Post

    public void setNames(String name) {
    for (String str : names)
    str = name;

    }
    another way would be

    Java Code:
    public void setNames(String name) {
    for (int i=0;i<names.length;i++)
    {
        names[i]=name;
    }
    }

  4. #4
    luckyleaf95 is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    it compiles okay. but it's a logic error. Notice how the output of names didnt change? it's supposed to change to "Sally." and eclipse just says that the local variable "str" is never read...

  5. #5
    luckyleaf95 is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    how do i not change the enhanced for loop and still get the same output?

  6. #6
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    the reason its not changing is because your just changing the value of str but not the value of array elements itself.

    remember str is just a temporary variable you created in the for loop, and your changing that value. Which doesnt sound right? right.

    Java copies and passes the reference by value, not the object. Thus, manipulation will alter the objects, since the references point to the original objects
    Last edited by [RaIdEn]; 02-03-2010 at 09:12 AM.

  7. #7
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    This should work
    Java Code:
    public void setNames(String name) 
    {
      int i=0;
     
       for (String str : names)
       {
               names[i++]=name;
        }
    }

  8. #8
    luckyleaf95 is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    yes it works. but how is the local variable str read in this case? it's never referred to in the code? and eclipse still uses a yellow underline to point out that it's never read. @@

  9. #9
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Quote Originally Posted by [RaIdEn] View Post
    the reason its not changing is because your just changing the value of str but not the value of array elements itself.

    remember str is just a temporary variable you created in the for loop, and your changing that value. Which doesnt sound right? right.

    Java copies and passes the reference by value, not the object. Thus, manipulation will alter the objects, since the references point to the original objects
    Here i edited my previous message.

    One way of using that loop is to print things as you effectively did in your print method.

  10. #10
    luckyleaf95 is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 12-05-2009, 12:24 PM
  2. Your variable is never read
    By singularity in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 01:13 PM
  3. Read and draw a local image.
    By ichwar in forum New To Java
    Replies: 1
    Last Post: 07-27-2009, 03:02 PM
  4. How to read xml file located in local system using Java
    By java_kick in forum Advanced Java
    Replies: 2
    Last Post: 04-07-2009, 12:28 AM
  5. Replies: 2
    Last Post: 03-31-2008, 06:11 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
  •