2

So i am having trouble wrapping my head around an issue, current working on a midi player that reads a file using buffered reader. i am reading the each object from the file as a string into an array list. Problem is when within the file there can be three different potential objects, the frequency of a note which is double, the midi note value an int, and a the note in letters(c4#). How can i tell which type of object exists in the string object from the ArrayList i have built.

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

The constructors are just simple

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

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

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

How do i differentiate between string or double objects from an array List of type string. I can't tell if i should change to ArrayList and make the object Note serializable or to just test the ArrayList element for a . or isLetter() and go from there or is there a more efficient way.

Kammryn Dancy
  • 135
  • 1
  • 8
  • A MIDI file is binary, not text. You shouldn't be using a `Reader` at all: you should be using an `InputStream`, probably `DataInputStream`. Or else it isn't a MIDI file at all. – user207421 Oct 19 '15 at 09:18
  • Sorry i should have been clear, the files being pulled in are not midi files they are just .txt files delimited with commas. I am just checking for the data type of each new string object split from the file. – Kammryn Dancy Oct 23 '15 at 03:26

1 Answers1

0

It seems that regex should do. Double is different from integer by floating point, test it with /^[0-9]+(\\.[0-9]+)?$ and as for the notes, this could help:

Regex for matching a music Chord

Community
  • 1
  • 1
Roman Pustylnikov
  • 1,937
  • 1
  • 10
  • 18