音符音符到音符的转换方法,运行到NumberFormatException

时间:2016-11-24 18:39:26

标签: java methods type-conversion numberformatexception

我正在研究一个方法,其参数是音程的字符串和String根音符。例如,音乐中的小音阶是以下间隔:

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递增,每次递增。

例如,如果输入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;
}

1 个答案:

答案 0 :(得分:0)

您不小心连接了不可转换的字符串:interval = interval.concat(c);(第31行)。我认为它应该是interval = interval.concat(c.charAt(0));