javaparsingarraylistmidimultiple-constructors

Problems parsing in objects from buffered file and sending to the proper constructors


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.


Solution

  • 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