2015-10-18 31 views
2

因此,我在解決一個問題時遇到了困難,目前正在使用緩衝讀取器讀取文件的MIDI播放器。我正在從文件讀取每個對象作爲一個字符串到數組列表中。問題是文件內可能有三個不同的潛在對象,一個音符的頻率是雙倍,midi音符的值是一個int,另一個是字母(c4#)的音符。我如何知道我已經構建的ArrayList中的字符串對象中存在哪種類型的對象。從緩衝文件解析對象併發送給正確構造函數的問題

ArrayList<String> noteList = new ArrayList<String>(); 
Note note; 
    String file = JOptionPane.showInputDialog("enter the file name of the song"); 
    String line; 
    try 
    { 
     BufferedReader bufread = new BufferedReader(new FileReader("res/"+file)); 
     while((line = bufread.readLine()) != null) 
     { 
      String[] result = line.split(","); 
      for(String str : result) 
      { 
       noteList.add(str); 
      } 
     } 
     bufread.close(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    for(int i=0; i<al.size();i++) 
    { 
     note = new Note(al.get(i)); //this is where the three constructors should be called 
     int midiInteger = note.getMIDIAbsoluteNumber(); 
    channels[15].noteOn(midiInteger,127);         
    Thread.sleep(400); 
    channels[15].noteOff(midiInteger,127); 
    } 

構造函數只是簡單的

public Note(double frequency) 
    { 
     frequency = this.frequency; 
    } 

    public Note(int noteNum) 
    { 
     noteNum = this.Note; 
    } 

    public Note(String letterNote) 
    { 
     letterNote = this.letterNote; 
    } 

我如何串或雙對象之間的區分字符串類型的數組列表。我不能告訴我是否應該更改爲ArrayList並使對象可序列化或僅測試ArrayList元素。或isLetter()並從那裏出發,或者有更有效的方法。

+0

MIDI文件是二進制的,而不是文字。你根本不應該使用'Reader':你應該使用'InputStream',可能是'DataInputStream'。否則它根本不是MIDI文件。 – EJP

+0

對不起,我應該清楚,被拉入的文件不是MIDI文件,它們只是用逗號分隔的.txt文件。我只是檢查從文件中拆分的每個新字符串對象的數據類型。 –

回答

0

看來,正則表達式應該做的。 Double不同於integer通過浮點,與/^[0-9]+(\\.[0-9]+)?$測試,並作爲註釋,這可能幫助:

Regex for matching a music Chord

+0

這一個也適用於我,當我在一些測試中運行它時,使用它來測試它是否與它相匹配 –