-2

I've been trying to figure out a way to print out values for an enhanced for loop statement for awhile now and just can't quite seem to figure out how to do it. I understand the basic concepts of an enhanced for loop but I can't seem to figure out how to use it in my scenario.

Here is the code:

 import java.util.ArrayList;

public class userGroup {

    ArrayList<User> userGroup = new ArrayList<>();
    User user0;

    public void addSampleData() {

    userGroup.add(user0);

    }

    public void printusername(){

       for (User x: userGroup)
           System.out.println(x);
    }
}

How would I implement a for loop statement in the printusername method that prints out user0 and its values? I understand that it's probably because there is an error somewhere in the creation of the ArrayList but I just can't find out where the error(s) are. Any help would be greatly appreciated by me and anybody else who may have the same issue. Thanks.

  • 1
    one obvious issue is.. your user0 object is a null reference, so eventually you are adding a null to your arraylist – mhasan Nov 01 '16 at 19:44
  • 1
    1. You should be using generics: `ArrayList`. 2. `getUser` probably does not do what you think it does. 3. Post what you have tried with the `for` loop. – bradimus Nov 01 '16 at 19:44
  • Don't name your class the same as one of its members. Also, in Java the convention is that class names have each word start with uppercase, e.g. UserGroup instead of userGroup. – azurefrog Nov 01 '16 at 19:51
  • Related: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – JimmyB Nov 01 '16 at 19:53
  • also idiomatic code uses camelCase like "printUserName". But then why do you have a class called *_group_ with an ArrayList which also has a single user member? Try to post clean/sensible code, so it can be useful for others as well instead of beeing confusing. – Wolf Nov 01 '16 at 19:57
  • I planned on creating multiple users in that group but I read that its bets practice to use only one example when asking questions on stack overflow. I've also changed the class name to UserGroup and edited the code whilst trying a for loop. I'll post the updates. – Creative Username Nov 01 '16 at 20:16
  • 1
    Have you implemented `User#toString()`? – bradimus Nov 01 '16 at 20:20
  • Not at the moment – Creative Username Nov 01 '16 at 20:22
  • 1
    Do that and initialize `user0` to something other than `null` and you should be good. – bradimus Nov 01 '16 at 20:31
  • You’re very close. Your creation of the ArrayList is entirely correct . Your use of the enhanced for loop is entirely correct. The issue is you’re adding a null, not a `User` object, so I assume your loop prints `null`. To have the values of the user printed, you need to create an object. If you aren’t already overriding `toString()` in the class `User`, you need to do that too (or change the `System.out.println()` to print selected values from `x`). – Ole V.V. Nov 01 '16 at 20:40

1 Answers1

0

I figure that you're close enough to an answer, but let's make the case a little simpler. Let's show a similar case with just one method to get some points across.

    public void printUsername() {  //note the case.  this is by convention in Java.

        //The left side is using the Collection interface, not an ArrayList.  
        //This is the weakest interface that we can use.   
        //We are also using generics to show that we have a Collection of User objects.

        //Lastly, we include the number one to show that we are 
        //creating an ArrayList with an initial capacity of 1.  We are just
        //adding one object.

        Collection<User> userGroup = new ArrayList<>(1); 

        //We create an object by using the "new" keyword
        User user0 = new User(); 

        //and now we add it to the Collection we created earlier
        userGroup.add(user0); 

        //now iterate through the loop
        for (User user: userGroup) {  //note the use of braces for clarity
            //this will call toString on the User object
            //if you don't implement toString(), 
            //you'll get a rather cryptic address (technically, a hashcode of the object)

            //try to make your variable names mean something too.
            //it's much easier to track down issues later when your variable
            //names make sense.  And it's much easier for the next person
            //trying to make sense of your code.
            System.out.println(user);  
        }
    }

If you are using Java 8 or later, you can replace your enhanced for loop with a collect call:

    userGroup.forEach(System.out::println);
riddle_me_this
  • 8,575
  • 10
  • 55
  • 80