2017-04-05 67 views
-1

我正在学习java编程的小课程,我正在为我的任务创建一个音乐盒。用户将输入一个字符串,它是'音乐笔记',但它可能包含与音乐笔记无关的其他字符(又名So-Fa名字,DRMFSLT)。我必须过滤用户输入字符串,并在大写字母中返回包含正确音乐笔记的字符串。如何过滤用户输入字符串并存储新的过滤数组

例如,用户输入 'dř p 中号 $ ˚F q 小号瓦特大号吨' 返回 '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)); 
} 

然而,当我运行它,没有输出显示。它根本不起作用,我不知道为什么。有人能够启发我,并建议一个更好的方法来做到这一点?谢谢。

+1

在我看来,您从来没有将任何东西置于干净的位置 – Ishnark

+0

为商店编辑您的赋值,因为它的大小数组而不是元素编辑它是'notes.length()'而不是'notes.length() - 1' – abcOfJavaAndCPP

+0

是啊我知道它是数组的大小,但数组的索引从0开始,所以数组的大小应该是length-1。我错了吗?无论如何,我会改变它notes.length(),它似乎更准确。 – issy

回答

0

你实际上并不需要,如果你想将它转换为大写字母,在修改后的宋级,我创建

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的字符数组自动填充仔细看创造有效的二维数组如果数组元素为空,则数值为'\ u0000',因此存储区中的空值将为'\ u0000'

+0

哦,这是一个更有组织这种方法来编码!谢谢你的建议!但空数组元素的问题仍然存在......我已经找到了从char数组中删除空元素的方法,并且人们建议将char数组转换为一个字符串并使用** replaceAll和trim函数** OR ** ArrayUtils.removeElements **。这是删除空数组元素的好方法吗? – issy

+0

空数组元素出现在哪里? – abcOfJavaAndCPP

+0

当'clean'使用'actualLetter','actualLetter'初始化时,等于'字符串笔记'中的所有字符,它不会被过滤并且包含不属于So-Fa名字的字符。当So-Fa名字存储在'clean [a]'中时,会有b e空数组元素。 – issy

0

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

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

所以这就是一切都在下坡的地方。非常感谢您发现问题! – issy

+0

感谢和欢迎到@ jssy。如果你发现任何有用的答案,你可以接受/ upvote。 – stinepike

0

你根本就没有存储有效的笔记。相反:

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

写:

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

所以这就是为什么它不起作用!感谢您发现错误! – issy

0

你从来没有真正放在任何阵列内部的清洁。你定义了它的长度并初始化了它,但它只是空的空间。尝试修改代码并包括 clean [x] = //其他char;

+0

感谢您发现问题。没有意识到那个愚蠢的错误:( – issy

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

注:我创建运行您的程序的主要方法。

+0

主要的方法是在另一个类中编码的,所以它不在这里哈哈,OMG谢谢你发现了分配问题,我没有意识到我犯了这个愚蠢的错误 – issy