【问题标题】:how to filter an user input string and store the new filtered array如何过滤用户输入字符串并存储新的过滤数组
【发布时间】:2017-04-05 16:46:30
【问题描述】:

我正在学习关于 Java 编程的次要课程,并且正在完成我创建音乐盒的任务。用户将输入一个字符串,即“音符”,但它可能包含与音符无关的其他字符(又名 So-Fa 名称,DRMFSLT)。我必须过滤用户输入字符串并返回一个包含正确音符的字符串,以大写形式。

例如,用户输入 'DirpM$FqswL2t' 返回 'DRMFSLT'。

这是我目前所拥有的:

public class Song {

String notes;

public Song(String music) {
    notes = music;
    char[] store = new char[notes.length()-1];

    for (int i = 0; i < notes.length(); i++) {
        store[i] = notes.charAt(i);
    }

    char[][] valid = {{'D','R','M','F','S','L','T'},{'d','r','m','f','s','l','t'}};

    char[] clean = new char[store.length];

    int a = 0;
    for (int i = 0; i < store.length; i++) {
        for (int j = 0; j < valid.length; j++) {
            for (int k = 0; k < valid[0].length; k++) {
                if (store[i] == valid[j][k]) {
                    valid[0][k] = clean[a];
                    a++;
                }
            }
        }
    }

    notes = String.valueOf(clean);
    System.out.print(String.valueOf(clean));
}

但是,当我运行它时,没有显示输出。它根本不起作用,我不知道为什么。有人可以启发我并提出更好的方法吗?谢谢。

【问题讨论】:

  • 在我看来你从来没有真正把任何东西放在干净的地方
  • 编辑您为 store 分配的值,因为它是数组的大小,而不是元素将其编辑为 notes.length() 而不是 notes.length()-1
  • 是的,我知道它是数组的大小,但是数组的索引从 0 开始,所以数组的大小应该是长度为 1。我弄错了吗?反正我会把它改成notes.length(),看起来确实更准确。

标签: java arrays string input netbeans


【解决方案1】:

如果您想将其转换为大写字母,您实际上不需要创建一个有效的二维数组,请仔细查看我创建的修改后的 Song 类

   public class Song 
{

    private String notes;

    public Song(String music) 
    {
        notes = music;
        char[] store = new char[notes.length()];
        int actualLetter=0;
        //loop for consuming the letters
        for (int i = 0; i < notes.length(); ++i) 
        {
            if(Character.isLetter(notes.charAt(i)))
            {
                //converts a letter to uppercase
            store[i] = Character.toUpperCase(notes.charAt(i));

            }
        }
        //a valid array that is one dimensional
        char[] valid = {'D','R','M','F','S','L','T'};





            //looping for getting the actual size of clean
            for (int i = 0; i < store.length; ++i) 
            {
                for(int j=0;j<valid.length;++j)
                if (store[i] == valid[j]) 
                {
                    actualLetter++;
                }
            }
            char[] clean = new char[actualLetter];
            int a=0;
             //loop for getting equivalent letters
            for (int i = 0; i < store.length; ++i) 
            {
                for(int j=0;j<valid.length;++j)
                if (store[i] == valid[j]) 
                {
                    clean[a]=store[i];
                    a++;
                }
            }
            // output the values
        for(char clense:clean)
        {
            System.out.println(clense);
        }
    }
}

如果数组元素为空,则在 java char 数组中也会自动填充值 '\u0000',因此存储中的空值将是 '\u0000'

【讨论】:

  • 哦,这是一种更有条理的编码方式!感谢您的建议!但是空数组元素的问题仍然存在......我一直在寻找从 char 数组中删除空元素的方法,人们建议将 char 数组转换为字符串并使用 replaceAll 和 trim 函数ArrayUtils.removeElements。这是删除空数组元素的好方法吗?
  • 空数组元素在哪里出现?
  • cleanactualLetter初始化时,actualLetter等于String notes中的所有字符,它没有被过滤并包含不属于So-Fa名称的字符。当 So-Fa 名称存储在clean[a] 中时,会有空数组元素。
  • @issy 为了您的方便,我已经编辑了我的答案我添加了另一个循环来测试 So-Fa 名称的实际字母如果您认为这可以解决您的问题,请考虑接受它作为其他人未来参考的答案
【解决方案2】:

您没有在输出缓冲区中存储任何内容。您的代码的最短修复可能在最内部的循环中。 (不考虑所有其他改进)

                if (store[i] == valid[j][k]) {
                    clean[a] = valid[0][k];
                    a++;
                }

【讨论】:

  • 这就是一切都走下坡路的地方。非常感谢您发现问题!
  • 感谢并欢迎@jssy。如果您发现任何有帮助的答案,您可以接受/投票。
【解决方案3】:

您只是没有将有效的笔记存储在 valid 中。而是:

 valid[0][k] = clean[a];

写:

clean[a] = valid[0][k];

【讨论】:

  • 所以这就是它不起作用的原因!感谢您发现错误!
【解决方案4】:

您实际上从未在数组中放置任何内容。您定义了它的长度并对其进行了初始化,但它只是空白空间。尝试修改代码并包括 clean[x] = //其他字符 ;

【讨论】:

  • 感谢您发现问题。没有意识到那个愚蠢的错误:(
【解决方案5】:
public class Song {

    String notes;

    public Song(String music) {

        notes = music;
        char[] store = new char[notes.length()];

        //Here you could have a ArrayIndexOutOfBoundsException because the size of the arrays isn't the same
        for (int i = 0; i < notes.length(); i++) {
            store[i] = notes.charAt(i);
        }

        char[][] valid = { { 'D', 'R', 'M', 'F', 'S', 'L', 'T' }, { 'd', 'r', 'm', 'f', 's', 'l', 't' } };

        char[] clean = new char[store.length];

        int a = 0;
        for (int i = 0; i < store.length; i++) {
            for (int j = 0; j < valid.length; j++) {
                for (int k = 0; k < valid[0].length; k++) {
                    if (store[i] == valid[j][k]) {
                        //This assignation was wrong, you were assignating it to 'valid' array but it must be on clean which will contain the clean notes 
                        clean[a] = valid[0][k];
                        a++;
                    }
                }
            }
        }

        notes = String.valueOf(clean);
        System.out.print(String.valueOf(clean));
    }

    public static void main(String[] args) {
        Song song = new Song("DirpM$FqswL2t");
    }
}

输出: DRMFSLT

注意:我创建了一个 main 方法来运行您的程序。

【讨论】:

  • main方法在另一个类中编码所以这里就不放了哈哈。 OMG 感谢您发现分配问题。我没有意识到我犯了那个愚蠢的错误。
猜你喜欢
  • 1970-01-01
  • 2018-11-22
  • 2022-01-16
  • 2016-01-11
  • 2011-02-15
  • 2017-07-15
  • 2020-10-29
  • 1970-01-01
  • 2015-11-16
相关资源
最近更新 更多