2016-11-24 4 views
1

パラメータが楽音間隔のStringとStringのルートノートであるメソッドを処理しています。NumberFormatExceptionを実行して音符の音程を変換する方法

1 2 b3 4 5 b6 b7 8と与えられた根音Cを出力する必要があります:たとえば、音楽におけるマイナースケールは、以下の間隔で

C4 D4 Eb4 F4 G4 Ab4 Bb4 C5 

私もすべて変更された区間を含めるようにしようとしています、ex: b9, #9, b5, b11, b13.ベースインターバルが1オクターブより大きい場合は、それが4オレンジに及ぶオクターブを追加する必要があります。理論的には、メソッドは非常に大きなインターバルを、例えば1400のインターバルのようなノートに処理することができます。人間の聴覚の範囲を超えています。

どうすればよいですか?処理のために個々の間隔を格納するためのローカル変数String 'interval'、ハーフステップで間隔値を保持するローカル変数 'noteValue'、変換されたノートを格納するローカル変数 'notes'を作成しました。私はすべての区間の入力文字列を取り、forループの各位置iに対して部分文字列 "c"を作成します。私は、その部分文字列中の文字が数字0-9、 'b'(フラットサイン)、または '#'(シャープサイン)かどうかをテストします。そうでなければ、スペースに遭遇したとみなされ、ローカル変数 'interval'は処理に移ります。

文字列cの文字が0-9の数字の場合、それは文字列間隔に追加されます。文字列cの文字が 'b'の場合、noteValueはデクリメントされ、 '#'の場合、noteValueが1ずつインクリメントされます。

例えば、b13間隔が入力された場合、 'b'は処理され、noteValueを1減らします。名前間隔の文字列に '1'を追加する必要があります。次に、 '3'が読み取られ、名前間隔の文字列に追加されます。部分文字列cの文字が「b」または「#」の数字でない場合、「13」の値を持つ文字列間隔が半分に変換されます。それは 整数に解析され、メジャースケールの半ステップ値(すべてのインターバルが比較される)に従って変換されます。 b13のハーフステップ値は、オクターブ(12)+ a b6(8)=​​ 20でなければなりません。

根音がCであると仮定すると、b13はAbを返します。これは一連のifステートメントを使用して実行され、適切なルートノートを検索します。それぞれのルートノートにはスイッチが含まれています。 0〜11の可能なハーフステップ位置から音符値を見つけるために%12操作を適用することによって、音符値が決定され、音符ストリングに追加されます。 intervalStringパラメータ全体が処理されると、notes Stringが返されます。

私は 'interval' StringをIntegerに解析しようとする38行目のNumberFormatExceptionに問題があります。 println()ステートメントを実行して何が起こっているかを確認しました。文字列の間隔が正しく更新されていないようです。ここからどこに行くのかは分かりませんが、どんな助けでも大歓迎です!あなたが誤って非変換可能文字列をCONCAT

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; 
} 

答えて

0

interval = interval.concat(c);(ライン31)

は、ここに私のコードです。私はそれがすべきだと思いますinterval = interval.concat(c.charAt(0));

関連する問題