1

so I have this task where I must enter two strings and after that I have to find what are there common letters,and then write them out but only once..so for example if the string1 is "onomatopoeia" and string2 is "conversation" I should get back: o,n,a,t,e,i... My only problem is the last part("I don't know how to write the letters only once)

here is my code

import java.util.Scanner;
import java.util.Arrays;

public class Zadatak4 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        char niz[] = new char[100];
        char niz2[] = new char[100];

        System.out.print("Add the first string: ");
        niz = scan.nextLine().toCharArray();

        System.out.print("Add the second string: ");
        niz2 = scan.nextLine().toCharArray();

        for (int i = 0; i < niz.length; i++) {

            for (int j = 0; j < niz2.length; j++) {

                if (niz[i] == niz2[j]) {
                    System.out.println(niz[i] + " ");

                    // What now!?!?!?
                }

            }
        }

    }

}
Smit
  • 4,685
  • 1
  • 24
  • 28

7 Answers7

2

Use a set:

LinkedHashSet<string> printNum = new LinkedHashSet<string>();
if(niz[i] == niz2[j])
{
      printNum.add( niz[i] );
}

// outside of loop
for( string s : printNum )
{
      System.out.println(s);
}
Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39
1

You could do this by utilizing two HashSets.

You have one hashset per word. When you encounter a letter in word1 you enter in set1. When you encounter letter in word2 you enter in set2.

Finally, you only keep the letters that are in both sets.

import java.util.HashSet;
public class Zadatak4 {

    /**
     * @param args
     */
    public static void main(String[] args) {


        Scanner scan = new Scanner(System.in);
        char niz[] = new char[100];
        char niz2[] = new char[100];

        System.out.print("Add the first string: ");
        niz = scan.nextLine().toCharArray();

        System.out.print("Add the second string: ");
        niz2 = scan.nextLine().toCharArray();

        HashSet<Integer> set1 = new <String>HashSet();
        HashSet<Integer> set2 = new <String>HashSet();


        for(int i = 0; i < niz.length; i++)
        {
            if(!set1.contains(niz[i]));
            set1.add((int) niz[i]);         
        }

        for(int i = 0; i < niz2.length; i++)
        {
            if(!set2.contains(niz2[i]));
            set2.add((int) niz2[i]);            
        }


        Iterator<Integer> it = set1.iterator();
        int currentChar;
        while(it.hasNext())
        {
            currentChar = it.next();
            if(set2.contains(currentChar))
            System.out.println((char)currentChar);
        }
    }

}
Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • Thanks but the answer from @Captain Skyhawk is much shorter and works the same... – Marko Bešlić Jun 21 '13 at 18:10
  • No Problem. I added a small optimization to makes niz.length comparisons/lookups, as opposed to niz.length * niz1.length which bloated the code. – Menelaos Jun 21 '13 at 18:15
1

in the innermost section of your for loop you're going to want to add them to a set

mutuals.add(niz[i])

then outside the loop at the beginning add this to declare it

Set<char> mutuals = new HashSet<char>()

make sure you do this OUTSIDE the loop

then afterwards, print out everything in mutuals

Stephan
  • 16,509
  • 7
  • 35
  • 61
1

Almost everyone suggesting Set, here is the hard way of doing it...

public static void main(String[] args) {

    String printed = "";

    Scanner scan = new Scanner(System.in);
    char niz[] = new char[100];
    char niz2[] = new char[100];


    System.out.print("Add the first string: ");
    niz = scan.nextLine().toCharArray();

    System.out.print("Add the second string: ");
    niz2 = scan.nextLine().toCharArray();


    for(int i = 0; i < niz.length; i++)
    {
        for(int j = 0; j < niz2.length; j++)
        {
                if(niz[i] == niz2[j])
                {                        
                    if(printed.indexOf(niz[i]) == -1) {
                           System.out.println(niz[i]+" ");
                    }

                    printed += niz[i];
                }
        }
    }
Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
1

What you need is an intersection of the two sets, so what you could use is Set.retainAll().

Kelvin Ng
  • 174
  • 1
  • 12
1

A one liner :

HashSet<Character> common =
    new HashSet<Character>(Arrays.asList(niz1)).retainAll(
        new HashSet<Character>(Arrays.asList(niz2)));
Dalmas
  • 26,409
  • 9
  • 67
  • 80
  • 1
    This is a better solution not because it's short, but because it avoids looping over the string. The question only really needs a Character iterable, not anything else, to be put into a string - not anything else. – einpoklum Jun 22 '13 at 13:24
0

Store the char in Set in

 Set<Character> cs=new HashSet<>();

 if(niz[i] == niz2[j])
 {
     cs.add(niz[i]); 
     //System.out.println(niz[i]+" ");

     //What now!?!?!?
 }
Masudul
  • 21,823
  • 5
  • 43
  • 58