如何过滤用户输入字符串并存储新过滤的数组

时间:2017-04-05 16:46:30

标签: java arrays string input netbeans

我正在学习java编程的小课程,我正在完成我创建音乐盒的任务。用户将输入一个字符串,该字符串是'音乐笔记'但它可能包含与音符无关的其他字符(又名So-Fa名称, DRMFSLT )。我必须过滤用户输入字符串并返回包含正确音乐笔记的字符串。

例如,用户输入' D i r p M $ F q 取值瓦特的 2的'返回' 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));
}

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

5 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您只是无法存储有效的有效注释。相反:

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

写:

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

答案 2 :(得分:0)

你从未真正在阵列中放置任何东西。你定义了它的长度并初始化它,但它只是空的空间。尝试修改代码并包含 clean [x] = //其他字符;

答案 3 :(得分:0)

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

注意:我创建了一个运行程序的主要方法。

答案 4 :(得分:0)

你实际上并不需要创建一个有效的二维数组,如果你想将它转换为大写字母,那么仔细看看我创建的修改过的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',如果数组元素为空,那么store中的空值将为'\ u0000'