簡體   English   中英

音符音符到音符的轉換方法,運行到NumberFormatException

[英]Conversion method for musical intervals to notes, running into NumberFormatException

我正在研究一個方法,其參數是一個音樂間隔的字符串和一個字符串根音符。 例如,音樂中的小音階是以下間隔:

1 2 b3 4 5 b6 b7 8並給出根音C輸出應為:

C4 D4 Eb4 F4 G4 Ab4 Bb4 C5

我試圖包括所有改變的間隔, ex: b9, #9, b5, b11, b13. 基本八度音程假定為4.如果音程大於八度音程,它應該添加多個八度音程,它跨越到4.理論上,該方法應該能夠將極大的音程處理成音符,例如1400的間隔,其中超過人類聽覺的范圍。

我怎么這么做? 我創建了一個局部變量String'interval'來單獨存儲每個間隔以進行處理,一個局部變量'noteValue'用於將間隔值保持為半步,一個局部變量'notes'用於包含轉換后的音符。 我獲取所有間隔的輸入字符串,並為for循環中的每個位置i創建子字符串“c”。 我測試該子字符串中的字符是數字0-9,'b'(平面符號)還是'#'(尖銳符號)。 否則,假設遇到了一個空格,局部變量'interval'繼續處理。

如果String c的字符是數字0-9,則將其添加到String間隔。 如果String c的字符是'b',則noteValue遞減,如果是'#',則noteValue遞增,每次遞增1。

例如,如果輸入b13間隔,則應處理'b'並將noteValue減1。 然后應將“1”添加到名稱間隔的字符串中。 然后讀取'3'並將其添加到名稱間隔的字符串中。 在子串c中的字符不是數字'b'或'#'之后,值為“13”的字符串間隔繼續轉換為半步。 它被解析為整數並根據主刻度的半步值(所有間隔與之比較)進行轉換。 b13的半步值應為八度(12)+ a b6(8)=​​ 20。

假設根音是C,則b13應該返回Ab。 這是使用一系列if語句來查找正確的根音符,每個音符都包含一個開關。 通過應用%12操作從0到11個可能的半步位置找到音符值,確定音符值並將其添加到音符字符串中。 處理完整個intervalString參數后,應返回notes String。

我遇到了第38行的NumberFormatException問題,我嘗試將'interval'字符串解析為整數。 我已經放置了一個println()語句來查看發生了什么,並且看起來String間隔沒有正確更新。 不確定從哪里開始,任何幫助將不勝感激!

這是我的代碼:

public class testIntervals {
public static void main(String[] args) {
    new testIntervals();
}
public testIntervals() {
    String intervals = "1 2 b3 4 5 b6 b7 8";
    String rootNote = "C";
    String notes = intervalsToNotes(intervals, rootNote);
    System.out.println(notes);
}
public String intervalsToNotes(String intervalString, String rootNote) {
    int noteValue = 0;
    int octave = 4;
    String interval = "";
    String notes = "";
    for (int i = 0; i < intervalString.length(); i++) {
        String c = intervalString.substring(i);
        System.out.println(c);
        if (c.charAt(0) >= '0' && c.charAt(0) <= '9')
            interval = interval.concat(c);
        else if (c.charAt(0) == 'b')
            noteValue--;
        else if (c.charAt(0) == '#')
            noteValue++;
        else {
            System.out.println(interval + c);
            int process = Integer.parseInt(interval);
            interval = "";
            for (int j = 1; i <= process; i++) {
                if (j%7 == 2 || j%7 == 3 || j%7 == 5 || j%7 == 6 || j%7 == 0) {
                    noteValue += 2;
                }
                else if (j%7 == 1 || j%7 == 4) {
                    noteValue++;
                }
            }
            octave += (noteValue / 12);
            if (rootNote.equals("Ab") || rootNote.equals("G#")) {
                switch (noteValue % 12) {

                case 0: notes = notes.concat(rootNote + octave + " ");break;
                case 1: notes = notes.concat("A" + octave + " ");break;
                case 2: notes = notes.concat("Bb" + octave + " ");break;
                case 3: notes = notes.concat("B" + octave + " ");break;
                case 4: notes = notes.concat("C" + octave + " ");break;
                case 5: notes = notes.concat("Db" + octave + " ");break;
                case 6: notes = notes.concat("D" + octave + " ");break;
                case 7: notes = notes.concat("Eb" + octave + " ");break;
                case 8: notes = notes.concat("E" + octave + " ");break;
                case 9: notes = notes.concat("F" + octave + " ");break;
                case 10: notes = notes.concat("Gb" + octave + " ");break;
                case 11: notes = notes.concat("G" + octave + " ");break;
                }
            }
            else if (rootNote.equals("A")) {
                switch (noteValue % 12) {

                case 0: notes = notes.concat(rootNote + octave + " ");break;
                case 1: notes = notes.concat("Bb" + octave + " ");break;
                case 2: notes = notes.concat("B" + octave + " ");break;
                case 3: notes = notes.concat("C" + octave + " ");break;
                case 4: notes = notes.concat("C#" + octave + " ");break;
                case 5: notes = notes.concat("D" + octave + " ");break;
                case 6: notes = notes.concat("D#" + octave + " ");break;
                case 7: notes = notes.concat("E" + octave + " ");break;
                case 8: notes = notes.concat("F" + octave + " ");break;
                case 9: notes = notes.concat("F#" + octave + " ");break;
                case 10: notes = notes.concat("G" + octave + " ");break;
                case 11: notes = notes.concat("G#" + octave + " ");break;
                }
            }
            else if (rootNote.equals("A#") || rootNote.equals("Bb")) {
                switch (noteValue % 12) {

                case 0: notes = notes.concat(rootNote + octave + " ");break;
                case 1: notes = notes.concat("B" + octave + " ");break;
                case 2: notes = notes.concat("C" + octave + " ");break;
                case 3: notes = notes.concat("Db" + octave + " ");break;
                case 4: notes = notes.concat("D" + octave + " ");break;
                case 5: notes = notes.concat("Eb" + octave + " ");break;
                case 6: notes = notes.concat("E" + octave + " ");break;
                case 7: notes = notes.concat("F" + octave + " ");break;
                case 8: notes = notes.concat("Gb" + octave + " ");break;
                case 9: notes = notes.concat("G" + octave + " ");break;
                case 10: notes = notes.concat("Ab" + octave + " ");break;
                case 11: notes = notes.concat("A" + octave + " ");break;
                }
            }
            else if (rootNote.equals("B") || rootNote.equals("Cb")) {
                switch (noteValue % 12) {

                case 0: notes = notes.concat(rootNote + octave + " ");break;
                case 1: notes = notes.concat("C" + octave + " ");break;
                case 2: notes = notes.concat("C#" + octave + " ");break;
                case 3: notes = notes.concat("D" + octave + " ");break;
                case 4: notes = notes.concat("D#" + octave + " ");break;
                case 5: notes = notes.concat("E" + octave + " ");break;
                case 6: notes = notes.concat("F" + octave + " ");break;
                case 7: notes = notes.concat("F#" + octave + " ");break;
                case 8: notes = notes.concat("G" + octave + " ");break;
                case 9: notes = notes.concat("G#" + octave + " ");break;
                case 10: notes = notes.concat("A" + octave + " ");break;
                case 11: notes = notes.concat("A#" + octave + " ");break;
                }
            }
            else if (rootNote.equals("B#") || rootNote.equals("C")) {
                switch (noteValue % 12) {

                case 0: notes = notes.concat(rootNote + octave + " ");break;
                case 1: notes = notes.concat("C#" + octave + " ");break;
                case 2: notes = notes.concat("D" + octave + " ");break;
                case 3: notes = notes.concat("D#" + octave + " ");break;
                case 4: notes = notes.concat("E" + octave + " ");break;
                case 5: notes = notes.concat("F" + octave + " ");break;
                case 6: notes = notes.concat("F#" + octave + " ");break;
                case 7: notes = notes.concat("G" + octave + " ");break;
                case 8: notes = notes.concat("G#" + octave + " ");break;
                case 9: notes = notes.concat("A" + octave + " ");break;
                case 10: notes = notes.concat("A#" + octave + " ");break;
                case 11: notes = notes.concat("B" + octave + " ");break;
                }
            }
            else if (rootNote.equals("C#") || rootNote.equals("Db")) {
                switch (noteValue % 12) {

                case 0: notes = notes.concat(rootNote + octave + " ");break;
                case 1: notes = notes.concat("D" + octave + " ");break;
                case 2: notes = notes.concat("D#" + octave + " ");break;
                case 3: notes = notes.concat("E" + octave + " ");break;
                case 4: notes = notes.concat("F" + octave + " ");break;
                case 5: notes = notes.concat("F#" + octave + " ");break;
                case 6: notes = notes.concat("G" + octave + " ");break;
                case 7: notes = notes.concat("G#" + octave + " ");break;
                case 8: notes = notes.concat("A" + octave + " ");break;
                case 9: notes = notes.concat("A#" + octave + " ");break;
                case 10: notes = notes.concat("B" + octave + " ");break;
                case 11: notes = notes.concat("C" + octave + " ");break;
                }
            }
            else if (rootNote.equals("D")) {
                switch (noteValue % 12) {

                case 0: notes = notes.concat(rootNote + octave + " ");break;
                case 1: notes = notes.concat("D#" + octave + " ");break;
                case 2: notes = notes.concat("E" + octave + " ");break;
                case 3: notes = notes.concat("F" + octave + " ");break;
                case 4: notes = notes.concat("F#" + octave + " ");break;
                case 5: notes = notes.concat("G" + octave + " ");break;
                case 6: notes = notes.concat("G#" + octave + " ");break;
                case 7: notes = notes.concat("A" + octave + " ");break;
                case 8: notes = notes.concat("A#" + octave + " ");break;
                case 9: notes = notes.concat("B" + octave + " ");break;
                case 10: notes = notes.concat("C" + octave + " ");break;
                case 11: notes = notes.concat("C#" + octave + " ");break;
                }
            }
            else if (rootNote.equals("D#") || rootNote.equals("Eb")) {
                switch (noteValue % 12) {

                case 0: notes = notes.concat(rootNote + octave + " ");break;
                case 1: notes = notes.concat("E" + octave + " ");break;
                case 2: notes = notes.concat("F" + octave + " ");break;
                case 3: notes = notes.concat("F#" + octave + " ");break;
                case 4: notes = notes.concat("G" + octave + " ");break;
                case 5: notes = notes.concat("G#" + octave + " ");break;
                case 6: notes = notes.concat("A" + octave + " ");break;
                case 7: notes = notes.concat("A#" + octave + " ");break;
                case 8: notes = notes.concat("B" + octave + " ");break;
                case 9: notes = notes.concat("C" + octave + " ");break;
                case 10: notes = notes.concat("C#" + octave + " ");break;
                case 11: notes = notes.concat("D" + octave + " ");break;
                }
            }
            else if (rootNote.equals("E") || rootNote.equals("Fb")) {
                switch (noteValue % 12) {

                case 0: notes = notes.concat(rootNote + octave + " ");break;
                case 1: notes = notes.concat("F" + octave + " ");break;
                case 2: notes = notes.concat("F#" + octave + " ");break;
                case 3: notes = notes.concat("G" + octave + " ");break;
                case 4: notes = notes.concat("G#" + octave + " ");break;
                case 5: notes = notes.concat("A" + octave + " ");break;
                case 6: notes = notes.concat("A#" + octave + " ");break;
                case 7: notes = notes.concat("B" + octave + " ");break;
                case 8: notes = notes.concat("C" + octave + " ");break;
                case 9: notes = notes.concat("C#" + octave + " ");break;
                case 10: notes = notes.concat("D" + octave + " ");break;
                case 11: notes = notes.concat("D#" + octave + " ");break;
                }
            }
            else if (rootNote.equals("E#") || rootNote.equals("F")) {
                switch (noteValue % 12) {

                case 0: notes = notes.concat(rootNote + octave + " ");break;
                case 1: notes = notes.concat("F#" + octave + " ");break;
                case 2: notes = notes.concat("G" + octave + " ");break;
                case 3: notes = notes.concat("G#" + octave + " ");break;
                case 4: notes = notes.concat("A" + octave + " ");break;
                case 5: notes = notes.concat("A#" + octave + " ");break;
                case 6: notes = notes.concat("B" + octave + " ");break;
                case 7: notes = notes.concat("C" + octave + " ");break;
                case 8: notes = notes.concat("C#" + octave + " ");break;
                case 9: notes = notes.concat("D" + octave + " ");break;
                case 10: notes = notes.concat("D#" + octave + " ");break;
                case 11: notes = notes.concat("E" + octave + " ");break;
                }
            }
            else if (rootNote.equals("F#") || rootNote.equals("Gb")) {
                switch (noteValue % 12) {

                case 0: notes = notes.concat(rootNote + octave + " ");break;
                case 1: notes = notes.concat("G" + octave + " ");break;
                case 2: notes = notes.concat("G#" + octave + " ");break;
                case 3: notes = notes.concat("A" + octave + " ");break;
                case 4: notes = notes.concat("A#" + octave + " ");break;
                case 5: notes = notes.concat("B" + octave + " ");break;
                case 6: notes = notes.concat("C" + octave + " ");break;
                case 7: notes = notes.concat("C#" + octave + " ");break;
                case 8: notes = notes.concat("D" + octave + " ");break;
                case 9: notes = notes.concat("D#" + octave + " ");break;
                case 10: notes = notes.concat("E" + octave + " ");break;
                case 11: notes = notes.concat("F" + octave + " ");break;
                }
            }
            else if (rootNote.equals("G")) {
                switch (noteValue % 12) {

                case 0: notes = notes.concat(rootNote + octave + " ");break;
                case 1: notes = notes.concat("G#" + octave + " ");break;
                case 2: notes = notes.concat("A" + octave + " ");break;
                case 3: notes = notes.concat("A#" + octave + " ");break;
                case 4: notes = notes.concat("B" + octave + " ");break;
                case 5: notes = notes.concat("C" + octave + " ");break;
                case 6: notes = notes.concat("C#" + octave + " ");break;
                case 7: notes = notes.concat("D" + octave + " ");break;
                case 8: notes = notes.concat("D#" + octave + " ");break;
                case 9: notes = notes.concat("E" + octave + " ");break;
                case 10: notes = notes.concat("F" + octave + " ");break;
                case 11: notes = notes.concat("F#" + octave + " ");break;
                }
            }
        }
    }
    return notes;
}

你不小心連接了一個不可轉換的字符串: interval = interval.concat(c); (第31行)。 我認為它應該是interval = interval.concat(c.charAt(0));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM