Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default how to form clusters of number pairs using java

    hi guys i am writing a program where it takes number pairs as input and form the clusters based on the common numbers between the pairs.This is the code i have written so far:
    Java Code:
    public class cluster 
    { 
    public static void main(String args[]) throws FileNotFoundException 
    { ArrayList> x = new ArrayList(); 
    ArrayList y = new ArrayList(); 
    ArrayList k=new ArrayList(); 
    ArrayList z=new ArrayList(); 
    ArrayList w=new ArrayList(); 
     
    System.out.println("how many"); 
    Scanner s=new Scanner(System.in); 
    int n=s.nextInt(); 
    for(int i=1;i<=n;i++) 
    { 
    System.out.println("enter pairs"); 
    int a=s.nextInt(); 
    int b=s.nextInt(); 
    if(!y.contains(a) && !y.contains(b)&& x.isEmpty()) 
    { y.add(a); 
    y.add(b); 
     
    x.add(y); 
    System.out.println(y); 
    } 
    else if(!y.contains(a) && !y.contains(b)&& !x.isEmpty() && k.isEmpty()) 
    { 
     
    k.add(a); 
    k.add(b); 
    System.out.println(k); 
    System.out.println(y); 
     
    } 
     
    else if(!y.contains(a) && !y.contains(b)&& !x.isEmpty() && !k.isEmpty() && z.isEmpty()) 
    { 
    if(k.contains(a)&& !k.contains(b)) 
    { 
    k.add(b); 
     
    System.out.println(k); 
     
    } 
    else if(!k.contains(a)&& k.contains(b)) 
    { 
    k.add(a); 
    System.out.println(k); 
    System.out.println(y); 
     
    } 
    else if(!k.contains(a)&& !k.contains(b)) 
    { 
    z.add(a); 
    z.add(b); 
    System.out.println(z); 
    System.out.println(k); 
    System.out.println(k); 
     
    } 
     
    } 
    else if(!y.contains(a) && !y.contains(b)&& !x.isEmpty() && !k.isEmpty() && !z.isEmpty()) 
    { 
    if(z.contains(a)&& !z.contains(b)) 
    { 
    z.add(b); 
    System.out.println(z); 
    } 
    else if(!z.contains(a)&& z.contains(b)) 
    { 
    z.add(a); 
    System.out.println(z); 
     
    } 
    else if(!z.contains(b) && !z.contains(a)) 
    { 
    w.add(a); 
    w.add(b); 
    System.out.println(w); 
    } }
    Now what my problem is if i am entering 5 distinct pairs i am able to get 4 distinct clusters but if my 5th pair is also a unique pair then my 5th pair is added to the 4th pair forming that as cluster.How can i write nested if loops if there are 1000 pairs .please guys help me how to make this code work even for 1000 pairs
    Input:
    1 2
    2 4
    3 5
    6 7
    8 9
    11 12
    Expected output:
    1 2 4
    3 5
    6 7
    8 9
    11 12
    My output:
    1 2 4
    3 5
    6 7
    8 9 11 12
    Last edited by priyankakusuma; 04-01-2013 at 01:06 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: how to form clusters of number pairs using java

    This code is pretty hard to read, due largely to the non-descriptive variable names you're using. I suggest using Objects to store the points instead of parallel Lists (if that's what you're even doing).
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 1
    Last Post: 02-16-2012, 09:24 AM
  2. Replies: 9
    Last Post: 05-11-2011, 06:12 PM
  3. Replies: 5
    Last Post: 04-01-2010, 03:57 AM
  4. How do I validate user input from forms with Java?
    By rickywh in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 06:49 AM
  5. Replies: 2
    Last Post: 04-09-2009, 10:46 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
  •