Hi there, I'm working on a small task to practice my (non-existing) java skills.
I'm trying to write a program that reads a text file and replaces every character with a .wav file containing a musical note.

A friend gave me some help, and with his help I made class that ended up playing the last character/.wav file of every word.
The code for this is here:

package teksttest222;


import java.io.File;
import java.io.FileReader;
import java.io.IOException;


public class bruktest {

          public bruktest()
          {
          }

          public void spillLydStrenge(String sounds)
          {
                    String[] oppdelt = sounds.split("(?<=.)");
                    Lydsekvens sekvens = new Lydsekvens(".wav");
                    for (int i = 0; i < oppdelt.length; i++)
                    {
                              if (oppdelt[i].equals(" ") && !oppdelt[i].equals(""))
                              {
                                        sekvens.leggTilLyd("mellomrom.wav");
                              }
                              else if (!oppdelt[i].equals(""))
                              {
                                        sekvens.leggTilLyd(oppdelt[i] + ".wav");
                              }
                    }
                    sekvens.run();
                    System.out.print(sounds);
          }


          public void spillTekstfil(String textfile) throws java.io.IOException
          {
                    java.util.Scanner scan1 = new java.util.Scanner(new java.io.File(textfile));
                    java.util.Scanner scan2 = new java.util.Scanner(new java.io.File(textfile));

                    int counter = 0;
                    String line;

                    while (scan1.hasNext())
                    {
                              line = scan1.next();
                              counter++;
                    }

                    String[] lines = new String[counter];
                    int linecounter = 0;

                    while (scan2.hasNext())
                    {
                              lines[linecounter] = scan2.next();
                              linecounter++;
                    }

                    for (int i = 0; i <lines.length; i++)
                    {
                       spillLydStrenge(lines[i]);
                    }
          }

}

This ends up playing the last character of every word, not really sure how to fix it. The classes it refers to outside the file is just a generic Java Sound API file and a launcher.


I tried some other solutions, and the closest I came was this:

package teksttest222;


import java.io.File;
import java.io.FileReader;
import java.io.IOException;


public class bruktest {

          public bruktest()
          {
          }

          public void spillLydStrenge(String sounds)
          {
                    String[] oppdelt = sounds.split("(?<=.)");
                    Lydsekvens sekvens = new Lydsekvens(".wav");
                    for (int i = 0; i < oppdelt.length; i++)
                    {
                              if (oppdelt[i].equals(" ") && !oppdelt[i].equals(""))
                              {
                                        sekvens.leggTilLyd("mellomrom.wav");
                              }
                              else if (!oppdelt[i].equals(""))
                              {
                                        sekvens.leggTilLyd(oppdelt[i] + ".wav");
                              }
                    }
                    sekvens.run();
                    System.out.print(sounds);
          }


          public void spillTekstfil(String textfile) throws java.io.IOException
          {
        String contents = "";

      File f = null;
      try
      {
          f = new File("test.txt");

          if (f.exists())
          {

      FileReader fr = null;
              try
              {
                  fr = new FileReader(f);
                  char[] template = new char[(int) f.length()];
                  fr.read(template);
                  contents = new String(template);
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
              finally
              {
                  if (fr != null)
                  {
                      fr.close();
                  }
              }
          }
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }

          long numChar = f.length();
          int countChar =0;
          while(countChar<numChar)
          {
               countChar++;
          }
       for (int i = 0; i < countChar; i++)
       {
        spillLydStrenge(contents);
       }
  }
}

This just plays the entire string over and over again since my countChar isn't done correctly.

I'm starting to run out of ideas, and would appreciate any help you could give me.
Just about to give up on this little task, but it would be nice to know the actual solution.

Thanks in advance for any help!

PS: Sorry for all the messy code!

Solved this myself, solution was really obvious. (It always is when you figured it out though!)

Move sekvens.run(); into the main function and remove the For-loop that launches spillLydstrenge.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.