Java, I have been working on this problem and have below what i have so far. I a
ID: 3755227 • Letter: J
Question
Java, I have been working on this problem and have below what i have so far. I am trying to use the text file that has a list of frequencys to play. I cannot use Scanner and have to use readAllDoubles. My code is below as well as the problem.
Write a program called PlaySong that uses the StdIn method readAllDoubles to read a file of double values representing sound frequencies into an array. It then iterates through the array and plays each frequency for one quarter second. Create and place in the data folder a file called a2song.txt and write your program so that it reads from that file.
import stdlib.StdAudio;
import stdlib.StdIn;
public class PlaySong {
public static double main(String[] args) {
StdIn.fromFile("data/a2song.txt");
double[] frequency = StdIn.readAllDoubles();
while(!StdIn.isEmpty()) {
}
return frequency;
}
}
Explanation / Answer
According to your question, you have problems with extracting Double Values from your text file and you wish to do it without using Scanner.
Try this once.
import java.io.*;
public class PlaySong
{
public static void main(String[] args)throws Exception
{
Double[] frequency= new Double[Integer.MAX_VALUE]; // This array holds the frequency
//Use File class to hold the path of the input file
File music_File = new File("data/a2song.txt");
//Use buffer reader to read the content of the file
BufferedReader br = new BufferedReader(new FileReader(music_File));
//Use a string variable to hold the Double value as the sequence of characters
String str;
int i=0;
//Now copy one line at a time into the the String variable
while ((str = br.readLine()) != null)
{
//Convert the string into Double type and store it in the frequency array
frequency[i]= Double.parseDouble(str);
i++;
}
//use your function to play the music from Frequency array
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.