Question: Im writing this code which will take to array's and write it in the a output file in java the problem is when I do
Im writing this code which will take to array's and write it in the a output file in java the problem is when I do this the first line from the array dose not get printed out to the file I can't figure out what the problem is.
import java.io.*;
public class Songs {
public static void main(String[] args) throws IOException { String[] songs = { "drivers license", "Up", "Go Crazy", "34+35", "Blinding Lights", "Save Your Tears", "Mood (fear. iann dior)", "Calling My Phone", "Positions", "Levitating (feat. DaBaby)" };
double[] lengths = { 4.02, 2.36, 2.56, 2.53, 3.20, 3.35, 2.20, 3.25, 2.53, 3.23 };
File outFile = new File("songs.txt");
PrintWriter output = new PrintWriter(outFile); output.println("Billboard Top 10 Tracks:"); printArrays(songs, lengths); output.close();
}
/** * Prints out the values of two arrays side-by-side * * @param array1 the first array to print * @param array2 the second array to print */ public static void printArrays(String array1[], double array2[]) throws IOException { File file = new File("songs.txt"); PrintWriter out = new PrintWriter(file); for (int i = 0; i < array1.length; i++) { out.print(array1[i] + "-length: " + array2[i] + " "); } out.close(); }
}
The output file suppose to look like this:
drivers license - length: 4.02
Up - length: 2.36
Go Crazy - length: 2.56
34+35 - length: 2.53
Blinding Lights - length: 3.20
Save Your Tears - length: 3.35
Mood (feat. iann dior) - length: 2.20
Calling My Phone - length: 3.25
Positions - length: 2.53
Levitating (feat. DaBaby) - length: 3.23
but my output look like this:
Billboard Top 10 Tracks: 02 Up-length: 2.36 Go Crazy-length: 2.56 34+35-length: 2.53 Blinding Lights-length: 3.2 Save Your Tears-length: 3.35 Mood (fear. iann dior)-length: 2.2 Calling My Phone-length: 3.25 Positions-length: 2.53 Levitating (feat. DaBaby)-length: 3.23
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
