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 2 of 2

Thread: for loop in java

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question for loop in java

    Hi,
    I am new here and beginner for java.
    I wrote a simple List program with for loop.

    public class UserDemo {
     
    	List<String> userNameList = new ArrayList<String>();
     
    	public UserDemo (int number, String prefix){
    		for(int i =1; i <= number; i++){
    			addUser(prefix +"."+ i);
    		}
    		System.out.println("User :" +userNameList );
     
    		int size = userNameList.size();
    		[B]for(int i=0; i<size-1; i++ ){
    			for(int j=i; j<size-1; j++){
    				makeFriend(userNameList.get(i),  
                                    userNameList.get(j+1));
    			}
    		}[/B]
    	}
     
    	public void addUser (String username){
    		userNameList.add(username);
    	}
     
    	public void makeFriend (String currentUsername, String  
            previousUsername){
    		System.out.println(currentUsername + " is friend of-> "  
                    +previousUsername);
    	}
     
    	public static void main(String[] args) {
    		UserDemo au = new UserDemo(5,"newjava");
    	}
    }

    After running this code i m getting output in this form

    User :[newjava.1, newjava.2, newjava.3, newjava.4, newjava.5]
    newjava.1 is friend of-> newjava.2
    newjava.1 is friend of-> newjava.3
    newjava.1 is friend of-> newjava.4
    newjava.1 is friend of-> newjava.5
    newjava.2 is friend of-> newjava.3
    newjava.2 is friend of-> newjava.4
    newjava.2 is friend of-> newjava.5
    newjava.3 is friend of-> newjava.4
    newjava.3 is friend of-> newjava.5
    newjava.4 is friend of-> newjava.5


    but i want output in this form

    User :[newjava.1, newjava.2, newjava.3, newjava.4, newjava.5]
    newjava.1 is friend of-> newjava.2
    newjava.1 is friend of-> newjava.3
    newjava.1 is friend of-> newjava.4
    newjava.1 is friend of-> newjava.5

    newjava.2 is friend of-> newjava.1
    newjava.2 is friend of-> newjava.3
    newjava.2 is friend of-> newjava.4
    newjava.2 is friend of-> newjava.5

    newjava.3 is friend of-> newjava.1
    newjava.3 is friend of-> newjava.2
    newjava.3 is friend of-> newjava.4
    newjava.3 is friend of-> newjava.5

    newjava.4 is friend of-> newjava.1
    newjava.4 is friend of-> newjava.2
    newjava.4 is friend of-> newjava.3
    newjava.4 is friend of-> newjava.5

    newjava.5 is friend of-> newjava.1
    newjava.5 is friend of-> newjava.2
    newjava.5 is friend of-> newjava.3
    newjava.5 is friend of-> newjava.4

    Please help me that what to write within for loop, so it will give output
    as mentioned.

    Thanks in advance!


  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: for loop in java

    Thanks its working now,
    I should use this.

    for(int i=0; i<size; i++ ) {
         for(int j=0; j<size; j++){ 
              if (i!=j)
                    makeFriend(userNameList.get(i), userNameList.get(j));
         }
         System.out.println();
     }

Similar Threads

  1. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM
  2. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM
  3. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM
  4. JAVA for loop
    By tazjaime in forum Loops & Control Statements
    Replies: 2
    Last Post: August 18th, 2009, 07:43 PM
  5. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM