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!?!?!?
}
}
}
}
}
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);
}
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);
}
}
}
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
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];
}
}
}
您需要的是两个集合的交集,因此可以使用Set.retainAll()
。
A one liner :
HashSet<Character> common =
new HashSet<Character>(Arrays.asList(niz1)).retainAll(
new HashSet<Character>(Arrays.asList(niz2)));
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!?!?!?
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.