简体   繁体   English

如何只打印某些字母

[英]how to print only certain letters

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) 所以我要执行此任务,在这里我必须输入两个字符串,然后我必须找到存在的常见字母,然后将它们写出来,但只能写一次。.例如,如果字符串1是“拟声词”而字符串2是“对话”我应该回去:o,n,a,t,e,i ...我唯一的问题是最后一部分(“我不知道怎么只写一次字母)

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 . 您可以通过利用两个HashSets来做到这HashSets

You have one hashset per word. 每个单词有一个哈希集。 When you encounter a letter in word1 you enter in set1. 当您在word1中遇到字母时,请输入set1。 When you encounter letter in word2 you enter in set2. 在word2中遇到字母时,请输入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 在for循环的最里面,您需要将它们添加到集合中

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... 几乎每个人都建议Set ,这是完成此任务的困难方法...

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中

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

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

     //What now!?!?!?
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java-如何在不使用!@#$%^&*()的情况下从0打印到z?&gt; &lt;“:{} |字母,仅包括字母和数字 - Java - How to print from 0 to z without !@#$%^&*()?><":{}| letters, only alphabet and numbers 如何仅打印字符串的某些部分? - How to print only certain sections of a string? 如何检查两个字符串是否具有相同的字母,但只打印一次常见的字母? - How can I check if two strings have the same letters, but only print the common letters once? 如何遍历二叉搜索树并仅打印某些元素(JAVA) - How to traverse Binary search tree and only print certain elements (JAVA) 创建仅包含特定字母和特定长度的随机字符串 - Creating a random string with only certain letters and a certain length 如何读取文本文件并将其存储/打印到控制台只有小写字母且没有空格? - How can I read a text file and store/print it to the console with only lowercase letters and no spaces? 如何使用 .contains 选择字母表中的某些字母 - how to use .contains to select certain letters of the alphabet 如何使用Java打印出字母? - How to use Java to print out letters? 如何以(0,10,6)的格式打印字母(DNA) - How to print letters(DNA) in a format of (0,10,6) 仅当字符串包含字母、数字、空格或逗号时才打印字符串 - Print String only if it contains Letters, or Numbers, a space or comma
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM