Hello,

I am having problems in bMethod. The program compiles but there is no output display for bMethod. I am trying obtain a set from aMethod and assign this set to a suitable local variable in bMethod. Which I think where the problem is. I want trying to display both duplicates (thats where I have used HashSet) and display unique names. Thanks in advance for your help.

public class ExampleClass  
{
    
   Set<String> names = new TreeSet<String>();

  public ExampleClass()
   {
      super();
      names = new TreeSet<String>();
   }  

public static void aMethod()
{
   
  String[] name1 = {"Amy", "Jose", "Jeremy", "Alice", "Patrick" };  
  String[] name2 = { "Alan", "Amy", "Jeremy", "Helen", "Alexi" };

  for (int i = 0; i < name1.length; i++)
     {
       names.add(name1[i]);
     }  
   System.out.print(names);
   System.out.println();

 for (int i = 0; i < name2.length; i++)
     {
       names.add(name2[i]);
     }  
   System.out.print(names);
   System.out.println();
}

public static void bMethod()
{
  
 Set<String> names = new HashSet<String>();
 
  for (String a : anagramTable)
  {
     if
     (!names.add(a))
     System.out.println("Duplicate detected: " + a);
 
      else
      {
         System.out.println(" " + names);
      } 
}

I do not think you have yet compiled the code, else you should get an error at the following line in aMethod() method:-

names.add(name2[i]);

The error should be 'Cannot refer non-static variable from static context.'. The names variable should also be declared static to be accessible from a static method.
Also I do not think any duplicates would be encountered in the bMethod() cause, names is already a Set, and the duplicate elements would have already been filtered out when you were adding the elements to the 'names' TreeSet in aMethod() method.

Not sure if you really want your methods to be static. I do not know how you call these methods. Maybe, taking 'static' keyword from method may be another way to make it work. By the way, I have no clue with anagramTable variable. Though, as stephen84s said, there may not be any duplicated detection if you call bMethod() after you have already added names because the duplicates would already be filtered out.

anagramTable variable is a typo error. Found one why of doing it. Something called Iterator. This removes duplicates.

Iterator itr = names.iterator(); 

       while( itr. hasNext() )
       {

         System.out.print(" " + itr.next());
      }

How do I show a dialog box to state duplicates names could be displayed in this dialog box. Also in aMethod the display output shows...

 Alice Amy Jeremy Jose Patrick //outputs String[] name1
 Alan Alexi Alice Amy Helen Jeremy  Jose Patrick // outputs String[] name1 and String[] name2

How do I get an output for String[] name1 and outputs String[] name2 in seperate lines. Something like this..

 Alice Amy Jeremy Jose Patrick 
 Alan Alexi, Alice, Amy, Helen, Jeremy

Put a newline character ("\n") at the end of the part of the string where you want the following string to go on the next line.

commented: didn't explain where +0
commented: Erasing ttboy04's negative +5

NormR1 - what? "Put a newline character ("\n") at the end of the part of the string where you want the following string to go on the next line." You need to make it clear where because I have used number of println in aMethod.

You need to make it clear where

Try using it to see what happens.

How do I get an output for String[] name1 and outputs String[] name2 in seperate lines

I am confused what this means? You put Strings on separate lines by using the newline character or the println() method.

public static void aMethod() {
  String[] name1 = {"Amy", "Jose", "Jeremy", "Alice", "Patrick" };  
  String[] name2 = { "Alan", "Amy", "Jeremy", "Helen", "Alexi" };

  for (int i = 0; i < name1.length; i++) {
    names.add(name1[i]);
  }  
  System.out.println(names);  // add to ExampleClass variable 'name'
                              // print out "Amy Jose Jeremy Alice Patrick" --> name1

  for (int i = 0; i < name2.length; i++) {
       names.add(name2[i]);   // <-- Here is where Set is filtering duplicated names
  }                           //     and add valid to your ExampleClass variable 'names'
  System.out.println(names);  // print out the merged, and there is NO way to separate
                              // them here
  // If you want to print each name, just println name1 and name 2.
  // I cannot remember what will be displayed using println() on a string array...
  // Please try it...
  // System.out.println(name1);
  // System.out.println(name2);
}

How do I show a dialog box to state duplicates names could be displayed in this dialog box.

What do you mean by 'dialog box'? Where is it??? Not sure... You mean in your iterator???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.